Django forms, form is always invalid

Yes, I did look at the other responses to similar questions. But I haven't found one that helps me, Ive looked at all the currently available solutions.

I am trying to enter a name into a textbox, and then hit submit, allowing me to create a new list of items (the items are irrelevant). But when I hit the submit button nothing happens. After many print statements, Ive deduced that the reason why is because the form.is_valid() function is returning false

if response.method == "POST":
    # returns a dictionary of information in the form
    form = CreateNewList(response.POST)

    print(form.errors)
    # if the form is valid, get the name attribute and create a new ToDoList with it
    if form.is_valid():
        n = form.cleaned_data["name"]
        t = ToDoList(name=n)
        t.save()

        return HttpResponseRedirect("/%i" % t.id)
else:
    form = CreateNewList()

return render(response, "main/create.html", {"form": form})

After reading some posts I found online, the next step I took was printing out the errors using forms.errors This is what I got from that print out

<ul class="errorlist"><li>check<ul class="errorlist"><li>This field is required.</li></ul></li></ul>

At this point, I have no clue what check is or does. One persons response online said that it is part of some dictionary and I have to do something like form['check'] = 1, but when I do that it says the dictionary is immutable or an invalid statement. I also tried form.data['check']=1, same thing

Here is the function that creates a new list

class CreateNewList(forms.Form):
    name = forms.CharField(label="Name", max_length=200)
    check = forms.BooleanField(label="Completed", required=True)


Read more here: https://stackoverflow.com/questions/67376103/django-forms-form-is-always-invalid

Content Attribution

This content was originally published by Wizard at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: