I hope you can help me in advance thank you very much, it turns out that I have a form that I need to fill with information from my database before sending it to the template, but it turns out that when I want to evaluate it using the POST method, when validating it with if form.is_valid (): it returns me a False and I do not know why, as I could solve it, then my code.
class CheckboxForm(forms.Form):
check_0 = forms.BooleanField(label="", required=False)
check_1 = forms.BooleanField(label="", required=False)
check_2 = forms.BooleanField(label="", required=False)
check_3 = forms.BooleanField(label="", required=False)
check_4 = forms.BooleanField(label="", required=False)
check_5 = forms.BooleanField(label="", required=False)
check_6 = forms.BooleanField(label="", required=False)
check_7 = forms.BooleanField(label="", required=False)
check_8 = forms.BooleanField(label="", required=False)
check_9 = forms.BooleanField(label="", required=False)
check_10 = forms.BooleanField(label="", required=False)
check_11 = forms.BooleanField(label="", required=False)
check_12 = forms.BooleanField(label="", required=False)
check_13 = forms.BooleanField(label="", required=False)
check_14 = forms.BooleanField(label="", required=False)
def __init__(self, school=None, *args, **kwargs):
super(CheckboxForm, self).__init__(*args, **kwargs)
if school:
print("entro")
values = InstitucionalValues.objects.all()
i = 0
for value in values:
name = "check_" + str(i)
v_school = value.school.all()
for v in v_school:
if v == school:
self.fields[name] = forms.BooleanField(label="", required=False, initial=True)
break
i = i + 1
And the post method:
class VistaFormulario(View):
def post(self, request):
form = CheckboxForm(request.POST)
if form.is_valid():