Answer the question
In order to leave comments, you need to log in
How to validate a form with MultipleChoiceField in Django?
Hello.
I created a form in Django, I upload a file into it and enter a couple of text fields, the form.is_valid() form validation passes, everything was fine until I added a couple of <select>
MultipleChoiceField
.
Here is what it gives me:
<ul class="errorlist"><li>visit_comparison<ul class="errorlist"><li>Enter a list of values.</li></ul></li><li>ctr_comparison<ul class="errorlist"><li>Enter a list of values.</li></ul></li></ul>
from django import forms
class xlsYandexDirect(forms.Form):
visit_comparison_options = [
(">","Больше"),
("<","Меньше"),
("==","Равно")
]
ctr_comparison_options = [
(">","Больше"),
("<","Меньше"),
("==","Равно")
]
file = forms.FileField(label="", widget=forms.FileInput(attrs={"accept":"application/vnd.ms-excel"}))
visit_count = forms.CharField(label="", widget=forms.TextInput(attrs={"class": "input", "placeholder": "0"}))
ctr_count = forms.CharField(label="", widget=forms.TextInput(attrs={"class": "input", "placeholder": "0"}))
visit_comparison = forms.MultipleChoiceField(label="", widget=forms.Select, choices=visit_comparison_options)
ctr_comparison = forms.MultipleChoiceField(label="", widget=forms.Select, choices=ctr_comparison_options)
def index(request):
if request.method == 'POST':
form = xlsYandexDirect(request.POST, request.FILES)
if form.is_valid():
return render(request, 'main/success.html', {'text': text})
else:
return render(request, 'main/error.html')
else:
form = xlsYandexDirect(use_required_attribute=False)
return render(request, 'main/index.html', {'form': form})
<select>
are selected. Answer the question
In order to leave comments, you need to log in
Decided!
Default components Select
are used for fields ChoiceField
and notMultipleChoiceField
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question