简体   繁体   English

FormView Django中的ValidationError

[英]ValidationError in FormView Django

What is the correct way of raising the ValidationError in a FormView and passing it to the template with reloaded form? FormView中引发ValidationError并将其传递给具有重新加载的表单的模板的正确方法是什么? Currently I have this: 目前我有这个:

class ProfileUpdateView(FormView):
    template_name = 'profile_update.html'
    form_class = UserDetailForm
    success_url = '/profile/'

    def form_valid(self, form):
        userdetail = form.save(commit = False)
        try:
            already_exist_info = UserDetail.objects.get(document_type=userdetail.document_type,
                series=userdetail.series, number=userdetail.number)
            raise forms.ValidationError("Document already exists in DB")
        except UserDetail.DoesNotExist:
            [... some stuff here ...]
            userdetail.save()
        return super(ProfileUpdateView, self).form_valid(form)

It works and I get the error page, but I would prefer to show the error in the template with reloaded form. 它可以正常工作,并且显示错误页面,但我希望在重新加载表格的模板中显示错误。 Moreover, is there a built-in way to get ValidationError in FormView ? 此外,是否有内置方法在FormView获取ValidationError I mean, without importing forms from django . 我的意思是,不从django导入forms

Thanx. 谢谢

EDIT 编辑

Well, I've decided to do all the trick in other manner - using clear() method. 好吧,我已经决定以其他方式完成所有工作-使用clear()方法。 So now I have this: 所以现在我有了这个:

views.py views.py

class ProfileUpdateView(FormView):
    template_name = 'profile_update.html'
    form_class = UserDetailForm
    success_url = '/profile/'

    def form_valid(self, form):
        userdetail = form.save(commit = False)
        #[... some stuff ...]
        userdetail.save()
        return super(ProfileUpdateView, self).form_valid(form)

forms.py 表格

class UserDetailForm(forms.ModelForm):
    class Meta:
        model = UserDetail
        exclude = ('user', )

    def clean(self):
            cleaned_data = super(UserDetailForm, self).clean()
            document_type = cleaned_data.get("document_type")
            series = cleaned_data.get("series")
            number = cleaned_data.get("number")
            try:
                already_exist_info = UserDetail.objects.get(document_type=document_type,
                    series=int(series), number=number)
                raise forms.ValidationError("Document already exists in DB")
            except:
                pass
            return cleaned_data

Everything seems to be fine according to docs , however this time the form just saves without any errors. 根据docs ,一切似乎都还不错,但是这次表单只是保存而没有任何错误。

Raising ValidationError in the form's clean method is the correct approach. 在表单的clean方法中提高ValidationError是正确的方法。

Your problem is that you are catching all exceptions, including ValidationError . 您的问题是您正在捕获所有异常,包括ValidationError If you change your code to catch a more specific exception, then it should work. 如果您更改代码以捕获更具体的异常,则它应该起作用。

try:
    already_exist_info = UserDetail.objects.get(
        document_type=document_type,
        series=int(series), 
        number=number,
    )
    raise forms.ValidationError("Document already exists in DB")
except UserDetail.DoesNotExist:
    pass
return cleaned_data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM