While using ajax with django form, getting error "Select a valid choice. That is not one of the available choices."
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)]