Hi, I am a beginner in Django and I am trying to generate a form with two dropdowns and get the values from the selections. These values will be used later for some calculations.
I followed some tutorials and tried to figure out how the forms work in Django. However, I believe that I must have some misunderstandings about some basic things in Django, as I can not get the selections correctly from the two forms returned by POST. In particular, when I pressed the button, the option that I got from the first form was always the same as the one from the second form. Therefore, I guess I didn’t correctly refer “request.POST” to the two forms… When I had only one dropdown, it worked fine.
In models.py, I defined the field “electronic_state” as a string, it will be used to generate the options for both two dropdowns:
class Main(models.Model):
id_state = models.AutoField(primary_key=True)
electronic_state = models.CharField(max_length=255)
t_e = models.FloatField(blank=True, null=True)
........
The form is defined as follows in forms.py:
class ElectronicStateForm(forms.Form):
electronic_state = models.ModelChoiceField(
queryset = Main.objects.all().order_by('t_e').values_list('electronic_state', flat=True),
widget=forms.Select(),
to_field_name="electronic_state",
)
views.py:
from .forms import ElectronicStateForm
def vibration_view(request):
electronic_state_initial_selected = ""
electronic_state_final_selected = ""
if request.method == "POST":
form_initial = ElectronicStateForm(request.POST)
if form_initial.is_valid():
electronic_state_initial_selected = form_initial.data["electronic_state"]
form_final = ElectronicStateForm(request.POST)
if form_final.is_valid():
electronic_state_final_selected = form_final.data["electronic_state"]
else:
form_initial = ElectronicStateForm()
form_final = ElectronicStateForm()
context = {
"form_initial": form_initial,
"form_final":form_final,
"electronic_state_initial_selected":electronic_state_initial_selected,
"electronic_state_final_selected": electronic_state_final_selected,
}
return render(request, "vibration.html", context=context)
The HTML file vibration.html to be rendered:
<form method="POST">
{% csrf_token %}
<label>The initial </label>
{{form_initial}}
<p></p>
<label>The final </label>
{{form_final}}
<button type="submit" >Calculate</button>
</form>
<p>Franck-Condon factors for the transition between {{electronic_state_initial_selected }} and {{ electronic_state_final_selected }}: </p>
The idea is that the user selects two “electronic states” from the two dropdowns. Then the “Fanck-Condon factor” will be calculated for these two electronic states, based on some parameters that will be later read from MySQL tables. The views.py above is just a test if the options are correctly returned – obviously, they are not, as you can see from the screenshot below, “electronic_state_initial_selected” somehow becomes
the same as “electronic_state_final_selected” when I pressed the button.
I guess that the names and IDs of the forms are somehow the same. I have tried to use form_initial = ElectronicStateForm( request.POST,prefix="form_initial")
, however, I got MultiValueDictKeyError (‘electronic_state’)…
I must have done something very stupid here, but I can not figure it out. Any suggestions would be really appreciated. Thanks in advance!!