Answer the question
In order to leave comments, you need to log in
Keeping previous values after page reload django?
I have a drop down list of countries:
models.py code:
from django.db import models
class CountriesView (models.Model):
name = models.CharField(max_length=100)
class IndexView(View):
def get(self, request, *args, **kwargs):
countries = CountriesView.objects.all()
context = {
'countries': countries,
}
return render(request, 'index.html', context=context)
def post(self, request, *args, **kwargs):
list_countries = []
if request.method == 'POST':
country = request.POST['name']
list_countries.append(country)
countries = CountriesView.objects.all()
# контекст
context = {
'countries': countries,
'list_countries': list_countries,
}
return render(request, 'index.html', context=context)
<body>
<form method="post">
{% csrf_token %}
<select name="name">
<option selected="selected">Страны</option>
{% for country in countries %}
<option>{{ country.name }}</option>
{% endfor %}
</select>
<input type="submit" value="Вести">
</form>
{% for c in list_countries %}
<p>{{ c }}</p>
{% endfor %}
</body>
Answer the question
In order to leave comments, you need to log in
You can do this with jquery, there's an append function that adds a boring element to the end of the selected element. for example like this:
jQuery("document").ready(function($){
$('body').on('click', 'input', function() {
$('p').append($('select').val());
});
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question