python - django print form errors in template -


i have forms.form

class registrationform(forms.form):     username = forms.charfield(max_length=20)     password1 = forms.charfield(max_length=20)     password2 = forms.charfield(max_length=20)     email = forms.emailfield(max_length=50)      def clean_password2(self):         password1 = self.cleaned_data.get("password1")         password2 = self.cleaned_data.get("password2")         if password1 , password2 , password1 != password2:             raise forms.validationerror(" 2 password didn't match")          return password2 

in view checking duplicate username or email.

if user.objects.filter(q(username=username) | q(email=user_email)).exists():     raise validationerror("username or email exist") 

now error not showing in form. showing on browser when debug true. dont want use model form.

need suggeston how print error in form

you need store validation logic inside form class. that's purpose of forms layer in django.

you can override clean method access both username , passwords fields data:

def clean(self):    cleaned_data = super(registrationform, self).clean()    username = cleaned_data['username']    user_email = cleaned_data['email']    if user.objects.filter(q(username=username) | q(email=user_email)).exists():         raise validationerror("username or email exist") 

any errors raised form.clean() not associated field in particular. probably, need add {{ forms.non_field_errors }} list such errors in template

read more: https://docs.djangoproject.com/en/1.8/ref/forms/validation/


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -