0

I am using django modelforms and formsets. But the problem I am facing is that when the form is not validated, instead of showing the erros on the html page it gives and error in the view.

View `

def create(request):         
    if request.method=="POST":                                                          
        foo= fooform(request.POST,prefix="fooform") 
        if foo.valid():
           #do stuff             
    else:
        foo= fooform(prefix="fooform")          
    return render(request,'property/create.html',{'foo':foo})

`

Problem:

When the form does not validate there is a obvious error in the view where in it unable to find the fooform. I want the html page to show the error-ed fields. What am I doing wrong?

Edit:

I think I have found the main issue. The issue is that the modelform is not applying its form validations at the browser level. Because of this the form is getting submitted even though it is not valid. As a result it is failing the validation and not finding the else part.

2 Answers 2

2

How about using this pattern instead:

def create(request):         
    foo = fooform(request.POST or None, prefix='fooform')
    if foo.valid():
        #do stuff             

    return render(request,'property/create.html', {'foo': foo})
Sign up to request clarification or add additional context in comments.

Comments

1

Your las line is wrong:

It say:

return render(request,'property/create.html',{'foo':fooform})

And should be:

return render(request,'property/create.html',{'foo':foo})

If this is not the issue, please, post errors messages and form code.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.