6π
β
The problem is that ChoiceField
requires the selected option to be in its choice set.
In the above code, the choices for semester
are dynamically updating via jquery. However, these choices are not the part of semester
βs choice set i.e. sem_choices
. Hence the problem.
To solve this problem, include the selected value in sem_choices
by using the request.POST
method.
In views.py:
form = loginForm(request.POST)
sem = request.POST.get('semester')
form.fields['semester'].choices = [(sem, sem)]
π€Rohini Choudhary
1π
Another solution is to over-ride the valid_value() method of the ChoiceField. If you are not worried about the possible values the form could return, then itβs as simple as this:
class AjaxChoiceField(forms.ChoiceField):
def valid_value(self, value):
return True
Or you could add more validation if needed.
π€Chuck
- Can't connect to server running inside docker container (Docker for mac)
- How to thumbnail static files?
- Django + mod_wsgi. Set OS environment variable from Apache's SetEnv
- Drawing graphs in Django
Source:stackexchange.com