Django overwrite clean method form

When overwriting a pure form method, how do you know if its validation fails in any of the fields? for example in the form below, if I overwrite a clean method, how do I know if the form has not passed validation in any of the fields?

class PersonForm(forms.Form): title = Forms.CharField(max_length=100) first_name = Forms.CharField(max_length=100) surname = Forms.CharField(max_length=100) password = Forms.CharField(max_length=100) def clean(self, value): cleaned_data = self.cleaned_data IF THE FORM HAS FAILED VALIDATION: self.data['password'] = 'abc' raise forms.ValidationError("You have failed validation!") ELSE: return cleaned_data 

thanks

+6
django django-forms
source share
4 answers

You can check if any errors have been added to the dict error:

 def clean(self, value): cleaned_data = self.cleaned_data if self._errors: self.data['password'] = 'abc' raise forms.ValidationError("You have failed validation!") else: return cleaned_data 

BONUS! You can check for errors in certain fields:

 def clean(self, value): cleaned_data = self.cleaned_data if self._errors and 'title' in self._errors: raise forms.ValidationError("You call that a title?!") else: return cleaned_data 
+10
source share

If your data is not verified, the form instance will not have the cleaned_data attribute p>

Django Doc on Access to Clean Data

Use self.is_valid() .

+4
source share

Here is a simple example of overriding clean() in django.forms.Form , as well as using django-braces for AnonymousRequiredMixin , to require that only anonymous users visit the Loing page:

 class LoginView(AnonymousRequiredMixin, FormView): """ Main Login. And Social Logins """ template_name = 'core/login.html' form_class = LoginForm success_url = reverse_lazy('blog:index') def get_success_url(self): try: next = self.request.GET['next'] except KeyError: next = self.success_url return next def form_valid(self, form): cd = form.cleaned_data user = auth.authenticate(username=cd['login_username'], password=cd['login_password']) if user: auth.login(self.request, user) messages.info(self.request, 'You are logged in.') return super(LoginView, self).form_valid(form) 
0
source share

Although its an old post, if you want to apply checks on more than 1 field of the same form / model, use clean() . This method returns the dictionary cleaned_data strong>.

To show errors to users, you can use the add_error(<fieldname>, "your message") method. This will show errors along with the field name, and not on top of the form. An example is shown below:

add_error() automatically removes the field from the dictionary cleaned_data strong>, you do not need to delete it manually. Also you do not need to import anything to use this.

documentation here

 def clean(self): if self.cleaned_data['password1'] != self.cleaned_data['password2']: msg = 'passwords do not match' self.add_error('password2', msg) return self.cleaned_data 

If you just want validation in the same form / model field to use clean_<fieldname>() . This method will take values ​​from the dictionary cleaned_data strong>, and then you can check for logical errors. Always return a value after completing the logic check.

 def clean_password(self): password = self.cleaned_data['password'] if len(password)<6: msg = 'password is too short' self.add_error('password', msg) return password 
0
source share

All Articles