P
P
Peter Pitaev2021-01-13 12:12:58
Django
Peter Pitaev, 2021-01-13 12:12:58

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>


And here is the code itself:

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})


I can't figure out how to pass form validation with MultipleChoiceField, it writes an error even though all <select>are selected.

Help pliz, the second day I can not figure it out, Google has wiped it to the holes.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Petr Pitaev, 2021-01-13
@XXocTT

Decided!
Default components Selectare used for fields ChoiceField and notMultipleChoiceField

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question