I searched for so long for a solution to this, but still can't find any. I have a big form in my template, which is actually composed of a bunch of model forms. One field in that big form is not part of a form, but is a single dynamic drop down menu populated from a table called "Institutions" in views.py as such: Institutions.objects.all()
Here is the part from views.py:
def submission_form(request):
institutions = Institution.objects.all()
if request.method == 'POST':
abstractform = AbstractForm(request.POST)
authorform = AuthorForm(request.POST)
# Here I want code: if selected institution is this, then do that
if abstractform.is_valid() and authorform.is_valid()
new_abstract = abstractform.save()
new_author = authorform.save()
else:
return render(request, 'records/submission_form.html', {'abstractform': abstractform, 'authorform': authorform, 'institutions':institutions })
This is the drop down in my template:
<select id="ddlInstititions">
<option value="%">---------</option>
{% for entry in institutions %}
<option value="{{ entry.id }}">{{ entry.name }}</option>
{% endfor %}
</select>
My question is: Is it possible to pass that selected entry.name to the view so I can use it there? If not, what do you recommend doing instead?
Any help would be much appreciated!
In order for any form element to be sent in the POST, you need to have a name
attribute. So it should be <select id="ddlInstititions" name="institutions">
.
What's passed to the view in the POST is the value
attribute of each option
element. Currently, you've set that to entry.id
, so it's the ID that will be in the POST. You can either use that to look up the Institution object and get the name, or you can change the form so you put entry.name
directly in the value attribute.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With