Answer the question
In order to leave comments, you need to log in
How to properly write a drop down list using a model?
I need to write a drop-down list and an ok button, when clicked, the selected item gets into the database. Here is the code for the drop down list.
index.html
<form action="file.php" method="get">
<select name="linksNav" size="1">
<option value="{% url 'useappl' useappl_id=p.id %}" selected >{{p.calendar}}</option>
</select>
</form>
def index(request): if request.user.is_authenticated(): t=Application.objects.get(status1=True)
return render(request, 'useappl/index.html', { 't' : t } )
else:
return HttpResponseRedirect(reverse('account.views.login'))
def useappl(request, useappl_id):
t=Application.objects.get(pk=useappl_id)
return HttpResponseRedirect(reverse('account.views.login'))
Answer the question
In order to leave comments, you need to log in
Read about forms in django , where examples explain how to make a drop-down list and a simple field and file upload.
The example that you found in general is from another steppe, django is the python language, and file.php is the php language, you will not find them in one project.
Here is a very stretched example of using modelform :
models.py
class Application(models.Model):
brand = models.ForeignKey('Brand', blank=True, null=True)
class ApplicationForm(ModelForm):
class Meta:
model = Application
fields = ['brand']
def useappl(request, useappl_id):
if request.method == 'POST':
form = ApplicationForm(request.POST)
if form.is_valid():
# Обработка
form.save() # сохранение модели
return HttpResponseRedirect('/thanks/')
else:
form = ApplicationForm()
return render(request, 'contact.html', {'form': form})
{{ form }} # выведет форму, с выпадающем списком из брендов, останется прилепить кнопку
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question