简体   繁体   中英

django how do you pass validation errors from views.py to forms.py

How do I raise/pass validation errors from views.py to forms.py?

Should I do:

e = sub_form.cleaned_data['code']           
try:
    cde = sample_table.objects.get(code=e)
    de.acts = 'yes'
    cde.save()
except:
    raise sub_form.ValidationError("invalid code")

Here's my form:

class confirmform(ModelForm):

    code = forms.CharField( max_length=15, label = ("Your Activation Code"))

    class Meta:
        model = sample_table
        fields = ('code',)

I know there is a clean/proper way of doing this but I have no idea how to do it and this code raises:

*object has no attribute 'ValidationError'*

Any advice would be welcomed and appreciated.

Use this bit of code

from django import forms

raise forms.ValidationError("Invalid code")

More info here:

https://docs.djangoproject.com/en/1.8/ref/forms/validation/#raising-validationerror

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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