Django User Registration with Email Confirmation
Generally, we sign up in any website, it sends the confirmation link to activate the account. Or sometime it asks to email to change the email or reset the password of an account.
In this tutorial, we will learn how to send the confirmation mail using the Django when someone registers on our web app. We will discuss an easy way to build this. But Django provides many other options like django allauth application, django-registration, django-registration-redux. Those applications make this task very straightforward.
Before starting, make sure that you must knowledge of Django and Python programming language. Create the basic setup of Django and configure the settings.
First we need to configure the email host server in the settings.py for confirmation mail. Add the below configuration in the settings.py file.
We used the email-id along with the password and gmail SMTP host server. You can use the other SMTP server as well.
We need to create the token that will be used in email confirmation URL. Create the token.py file in the token and add the below code.
We used the PasswordTokenGenerator class that is used to reset the password. In the above code, we generated the unique token for confirmation.
Create Registration Form
Django provides the built-in UserCreationForm which is an excellent class to create form. We create a forms.py file in the app and import the UserCreationForm class. Let's understand the following code.
In the above code, we imported the UserCreationForm and built-in User. Then we created a SignupForm including extra field email in SignupForm.
Here we create a view of sign up, it got information using POST method and valid it. We have used the commit = False because it allows us to get the model object and add some extra attribute. Here we have done user.is_active = False which means user cannot login until email is verified.
Then we used the EmailMessage() function to send mail along with the subject, message. Email message create by a template.
This template create an email body with activate link that will send for application.
Now we need to create a view for the activation link.
Create Activation View
Once the user click on the activation link, we need to activate their account through activation link. This process is handled by the activate view.
We have added the activate function after the signup function. This view will check token it valid then user will activate and login. We set user.is_active = True which means user can login.
Now we will map the views to the urls.
Create a sign up form in template/signup.html.
The form will look like as below. When the user click on the submit button the activation link send their entered email id.
When you click on the sign up button, the confirmation email send on given email-id.
Click on the received link and now you are ready to login.
Note -Make sure that the less secure app access setting should be turned on. Otherwise, you will face smtp error.
Tada, we have successfully created an email configuration based user registration. This process is straightforward and required less code to complete. You can make it more improvement by adding more functionality such as attractive template, redirect to login, etc.