Django where to add form validation

Hello. When using Django to signup a user, I want to do some validation checks. These include checking if the password fields in the form do not match, or if the username provided in the form is already taken.

Where do I perform these validation checks? Do I perform them in the actual signup form using the clean methods, and then just create the object in the view if the form is valid, or do I perform them in the actual signup view, and return an error to the template if they occur?

Which is the best way to do it? Thank you.

Start with Form Validation. Forms provide the facility to check fields individually and to supply a method that checks the form overall (e.g. compare fields to each other).

So yes, the standard way is to perform the validation within the forms.

@KenWhitesell Ok thanks so do all the possible validation you can do in the forms themselves right? Not the views? Even if I am working with data from the database (model instances)

Correct. Validating form data is a function of the form, not of the component rendering a form for display. In principle, you could use that form in multiple places - you want to same validation to apply to that form regardless of where it’s used.

@KenWhitesell Ah Thank you. So the view should only be used for receiving POST data, and displaying a form, and forms.py validates this POST data, as this is its job. Am I correct?

What the view does could be many things. A view may be responsible for both displaying the initial form to be filled out (handling the GET) along with receiving the data on the POST, as well as performing other functions not related to the form. (For example, if a form is creating a user, the view may have the responsibility for sending a welcome email to the newly-registered user.) So I wouldn’t say that displaying / receiving the form are the only functions of a view. But, I would agree that it is the form’s responsibility for handling / validating all the data in the form.

@KenWhitesell Ah ok. So for example, let’s say that I was implementing a signup functionality. The form will have 4 fields: username, email, password, confirm_password. So for this signup functionality, all I want to do is create a user, and redirect to a login page. The form itself will be responsible for doing all the validations using methods right? In this instance the validations are: checking if the username and email in the form are unique, and making sure the password fields match each other. And in the views file, all I would have to do is create the user and redirect to another page. This is the correct way to implement this right - as all the validation is done in the form.

Yes, this is the standard / typical way of doing it.

Note, I again avoid the use of “correct” or “best” - there is always the possibility of there being extenuating circumstances that lead you to a different solution. But I work from the idea that the “standard” or “usual” method is the best place to start. Your experience and needs will help you identify when the standard methods aren’t good enough.

@KenWhitesell Ah ok great, thanks for the help.