开发者

Django ModelForm validation and error messages

开发者 https://www.devze.com 2023-02-21 17:29 出处:网络
Sorry if this is a basic question: I am having trouble doing form validation with ModelForms in Django.

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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消