Question

Sorry if this is a basic question: I am having trouble doing form validation with ModelForms in Django.

The pattern I'm using is

def View(request):
    if request.method == 'POST':

        form = AddPageForm(request.POST)

        if form.is_valid:
            instance = form.save()  
            ...

        else:
            HttpResponse("Error")

This works fine if the form validates (the if... branch is followed.) When the form doesn't validate, I get a standard Django form validation error page; the else... branch is ignored.

Obviously, there must be something wrong/missing but I can't work out what from the official Django documentation. Any guidance would be appreciated.

Was it helpful?

Solution

missing () in if form.is_valid => if form.is_valid()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top