Django Creating Users using UserCreationForm

Django authentication framework provides a form named UserCreationForm (which inherits from ModelForm class) to handle the creation of new users. It has three fields namely username, password1 and password2 (for password confirmation). To use UserCreationForm you have to first import it from django.contrib.auth.forms as follows:

Unfortunately, Django doesn’t provide any view to handle the creation of users, so we have to create our own view.

In the cadmin app’s add the following URL pattern at the beginning of the urlpatterns list.


Create a view function called register() in cadmin’s as follows:


Next, create a new template register.html in cadmin app with the following code:


To view user registration form visit and you will get a page like this.

Create a new user by entering username and password. On success, you will be greeted with “Account created successfully” message.

If entered username already exists or passwords don’t match then the form will display errors like this:

It is important to note that user created using UserCreationForm will have is_superuser and is_staff set to False but is_active set to True.

The only drawback of UserCreationForm is that it doesn’t have email field. As a result, we can’t use it to send email verification to the user to verify the account.

Most of the time a user registration involves the following steps:

  1. The user fills the registration form and hit submit.
  2. The site sends an email verification link to the submitted email.
  3. User clicks on the activation link to verify the account.

At this point, we have two options:

  1. Extend the UserCreationForm to include email field and email verification capability.
  2. Create a completely new user registration form from scratch.

We will go with the second option because it allows us to control every aspect of user creation process. Further, it will also reinforce your understanding of working with the forms.

Let’s create a new class called CustomUserCreationForm in cadmin’s file and add the following code to it. If doesn’t already exists create the file now.


Here we are defining four fields namely username, email, password1 and password2; with their own clean_<field_name>() method (except for password1 field). Pay close attention to the widget keyword argument in both the password fields. The widget keyword argument allows us to change the default widget of the field. Recall that by default, CharField is rendered as text field (i.e <input type="text" ... >). To render the CharField as password field we have set widget keyword argument to forms.PasswordInput.

The clean_username() and clean_email() methods check for duplicate username and email respectively. The clean_password2() method checks whether the password entered in both the fields matches or not. Finally, the save() method saves the data into the database.

Open cadmin’s file and update register() view to use CustomUserCreationForm.


Our registration form is almost ready for now. Visit to create some new users.

If account creation succeeds then you will get a success message as follows:

On the other hand, if CustomUserCreationForm encounters any errors. They will be displayed in the form as follows:

Notice that CustomUserCreationForm inherits from forms.Form class rather than forms.ModelForm. We have deliberately done this to show you everything we have learned about the forms is still applicable. If we wanted we could just easily use form.ModelForm instead of forms.Form. This is not the final version of the registration form, we will update this form in the upcoming chapter.

Another thing you might have noticed is that we haven’t implemented email verification in our CustomUserCreationForm, the reason is that email verification requires us to store some additional data about the user. At this point, we have no way of storing additional data about the User. To do so, we have to extend our User model. We will see how it’s done in the next chapter.

Note: To checkout this version of the repository type git checkout 32a.

10 thoughts on “Django Creating Users using UserCreationForm

  1. Hey there,

    I tried your solution to create a custom user login from my custom user model. Unfortunately, it does not save the user. No error messages are displayed. May you have an idea what I did wrong?

    Thanke you!

  2. Oy thanks, that is really useful!

    I have a question. My project is blog. How do you think, it’d be better to make registration part as an app or just to past it to existing part of the project?

  3. Hello, I am getting this error, may i know what may be the cause..

    ValueError at /student_portal/newuser/

    invalid literal for int() with base 10: ”

Leave a Comment

%d bloggers like this: