Django UserCreationForm| Creating New User

Django comes with a build-in user authentication system. This configuration performs the most common requirements project needs, handling a wide range of tasks, and valid passwords and permissions.

We can create the user login by importing user authentication modules. This article will discuss about the UserCreationForm, which is used to create a new user. It is a build-in module inherits from the ModelForm class. Before learning the Django UserCreationForm, let's have a brief introduction of User.

What are User objects?

User objects are the main component of the user authentication system. They are represented as the site visitor and are used to enables thing like preventing access, registering user profiles, correlating content with creators, and many more. But the 'superuser' and 'admin' class user objects are provided the special attributes.

User objects consist of the following primary attributes.

  • username
  • password
  • Email
  • first_name
  • last_name

Implement Django UserCreationForm

Django UserCreationForm is used for creating a new user that can use our web application. It has three fields: username, password1, and password2(which is basically used for password confirmation).

To use the UserCreationForm, we need to import it from django.contrib.auth.forms.

Here, we will create the view to handle the request.

view.py file

Now we will create the path in Helllo.url.py to handle the request.

template/base.html

Next, we will create the register.html file by extending the base.html file in template folder.

template/register.html

When we send the request using http://127.0.0.1:8000/register/ we get the form as follows.

Output:

Django UserCreationForm

When we click on the Register button, the POST request will be generated and create the new User. The newly created User using UserCreationForm() will set is_superuser and is_staff as False but is_active set to True.

The UserCreationForm() provides the limited fields. Suppose we want to send the verification mail to verify the User; we cannot do that because it doesn't have an email field.

A proper user registration form should take the following steps.

  • The user fills in their details and hits submit button.
  • The sites send the verification link on submitted mail.
  • The user receives the verification link on their entered mail.

To do that, either we modify the UserCreationForm, including the email field and verification facility, or create a new user registration form from scratch.

We will create the new user registration form because it will provide complete control over the form.

Now, we will create the forms.py file and create the new class CustomUserCreationForm in sampleapp's forms.py file.

Example -

\python_project\Myfirstdjangoproject\Hello\sampleapp\forms.py

In the above code, we define four fields name username, email, password1 and password2 in CustomUserCreationForm. Next, we define individual method for each field to clean the data. If you look closely, the widget field is used in both password1 and password2 fields. The widget key argument allows to change the default widget of the fields.

  • The username_clean() will prevent to enter duplicate username.
  • The email_clean() will prevent to enter duplicate email.
  • The clean_password2() method will check both passwords are matched or not

And then, the save() method will save the data.

Now, we update the register() view in views.py file.

Output:

Django UserCreationForm

Here we enter the user details to create the new user, and we get the following output.

Django UserCreationForm

When you try to create the user with the same username, it will show a message - A user with that username already exists.

Django UserCreationForm

We can also extend this CustomeUserCreationForm by adding more fields and functionality and we can also use the form.ModelForm class. But in this tutorial, we have used the form.Form class. In the upcoming tutorial, we will implement the email verification property where user receives the mail for verification. To create an email verification facility, we need to store some additional data about the user, and in this tutorial we have no way to do so. We will do this upcoming User Model tutorial.