How to Create User Login and Registration on Your Website in Django



Python


In this article, we show how to create and set up user registration and login on a website in Django.

We will set this up using the django-registration-redux module, which allows user registration and login to be extremely simple.

Following this guide, you should be able to have user registration and login on your site in less than an hour.

So let's get right into it.


Install django-registration-redux

So the first thing you have to do is install django-registration-redux.

This can be done very simply with pip.

All you have to do is enter the following command in your computer's command prompt.



Once this has been successfully installed, then you have django-registration-redux and we can go onto the next steps.


Changes to Make in the settings.py File

So there's a number of changes you must make to the settings.py file in order for this to work.

The first thing you must do is add the app, 'registration', to the list of INSTALLED_APPS in the settings.py file.



This is needed to install the django-registration-redux registration app on your website.

Next, you must add the following 2 lines.



So what ACCOUNT_ACTIVATION_DAYS means is it sets the number of days that a user must activate their account by before no longer being able to do so.

The REGISTRATION_AUTO_LOGIN is a an optional parameter. The default value is equal to False. If this is True, users will automatically log in in when they click on the activation link in their email.

Next, we have to add in a line where the user will be redirected to after s/he has logged into your site.

This is a single line of code.



Besides specifying the hardcoded URL, you can also set this variable equal to the url name, identified in the urls.py page of the app that you want to bring the user to.

One last thing that you must do in the settings.py file is add email settings.

Usually, when a user registers on a website, s/he receives email confirmation of this, alerting that the registration was successful and that s/he needs to activate the account.

This the case for django-registration-redux.

So you need to set up an email so that this email account can send an email to the user.

In the example below, we set up a gmail account to have access to SMTP, which is what is need to send outgoing email.



So now we have a gmail account that can send email out to new registrants on the site.

You don't have to use gmail, but if you are using another email service provider, the settings that are specified above may change, such as the port number and the use of TLS, so check all the settings of that email service provider in order to set up SMTP on that email service provider successfully.

And these are all the changes needed for the settings.py file for django-registration-redux

To get a more in-depth article on this, see Changes to Make to the settings.py File for django-registration-redux.


urls.py File

Next, we have to add a line to the urls.py filie.

This line is shown below.



So your complete urls.py file may look something like that as shown below.



So this is the only change we need to make to the urls.py file.

Users would then be able to register by visiting the URL /accounts/register/, login (once activated) at /accounts/login/, etc.

However, this will not work yet, because there are still other things we need to set up before we can use these URLs.

To get a more in-depth article on this, see Changes to Make to the urls.py File for django-registration-redux.


base.html page

So now there are a number of pages that we need to create in order for this system to work.

django-registration-redux provides a lot of the built-in functionality needed for a user registration and login page on your website, but it's up to you to create many of the pages needed. We will go through all the pages needed now.

We start with the base.html page.

Since django-registration-redux version 1.2, a base.html file has been necessary in the templates directory in order for the whole system to work.

We'll first go over how to create the file, meaning what contents go into it. Then we will go over where to put it in your project directory.

So the contents of the file are extremely simple.

Basically all that can go into the base.html file is the following line shown below.



That single line is really all that is needed for the base.html file.

Basically the base.html file is just a file that needs to be present.

And all you have to do is write that it extends registration_form.html.

So this is all the content that really needs to compose the base.html file.

Now where does this file go?

So go into your top root directory.

Say, you created a project called mywebsite.

The highest directory will be called mywebsite, which is really an empty folder holder that just holds the project.

You then click this directory and then see another mywebsite directory, which is really the top root directory of your project. It is in this directory that we want to work with, the top level directory of the project.

So click this mywebsite directory.

In this folder, you create a templates directory.

Inside of this templates directory, you place the base.html file.

This takes care of the base.html file, but there's more that has to be done.

Inside of this templates directory, create another directory called registration.

Inside of this registration directory, you put all of the other files you need for django-registration-redux to work.

To get a more in-depth article on this, see base.html file needed for django-registration-redux.

This includes activate.html, activation_complete.html, activation_email.txt, activation_email_subject.txt, registration_complete.html, registration_form.html, login.html, and logout.html


activate.html

Now we need to create a page called activate.html

This is just a page alerting a user to the fact that there was a problem activating the account. This may occur if the user did something like wait pass the 7-day window that was required for activation to occur by.

So an example activate.html page is shown below.



So all this file does is state that there was a problem activating the account.

To download this page, click and save the following link: activate.html


activation_complete.html

Now we need to create a page called activation_complete.html

This is a page that tells a user that the account has been successfully activated.

An example activation_complete.html page is shown below.



To download this page, click and save the following link: activate.html


activation_email.txt

We now create a page called activation_email.txt

This is a plain text file that represents the body of the email that we send to a user once s/he has registered on our website.

The contents of this plain text file is shown below.



All of the variables in this file is filled in by django-registration-redux. So it takes care of a lot for us.

This email gives the link so that a user can activate the site and reminds a user that they can 7 days to activate (or whatever you specified in the 7 days).

To download this page, click and save the following link: activation_email.txt


activation_email_subject.txt

We now create a page called activation_email_subject.txt

This is a plain text file that represents the subject of the email that we send to a user once s/he has registered on our website.

The contents of this file is shown below.



So our subject will be, Activate your account at learningaboutelectronics.com

To download this page, click and save the following link: activation_email_subject.txt


registration_form.html

Next we create a page called registration_form.html

This is the form that contains the registration form that a user can register with.

The contents of this page is shown below.



So we have a standard form on this page. As with all forms in Django, {% csrf_token %} must be added for security purposes, to prevent cross-site request forgery. This prevents other sites from injecting data into the forms through methods such as POST commansds.

Notice with django-registration-redux, you don't need to create the form. All you need to do is put in the form, through the line, {{ form.as_p }}

We then add in a submit button that is of type, "submit" with the value of "Register"

So this creates our basic registration form of our website.

To download this page, click and save the following link: activation_email_subject.txt


registration_complete.html

Next, we create a form called registration_complete.html

This is a form that a user goes to once s/he has successfully completed registration.

The contents of this page is shown below.



We thank the user for registering on the site and state that a follow-up activation email should arrive shortly.

To download this page, click and save the following link: registration_complete.html


login.html

Next, we need a login.html file

This provides the login form for when a user has registered and now wants to log in.

The contents of this form is shown below.



It is very similar to the registration_form, except the value of the submit button is now, Log In

To download this page, click and save the following link: login.html


logout.html

Next, we need to create a logout.html page.

This is the page that a user is directed to when s/he logs out.

Below is the contents of a simple logout page.



Again, this is extremely simple. You may want to create something more complex, but this will do it just for demonstration purposes.

To download this page, click and save the following link: logout.html


template file

Lastly, we want to know how to alter the template file that a user sees.

In the template file, we add a Welcome message to the user.

We also add functionality to put in a log in link if no user is logged in and a logout link if a user is logged in (in case s/he wants to log out).

This is shown in the following code below for the template file that we render in the views.py file.

So in our body, we check if a user is authenticated (logged in) using the line, {% if request.user.is_authenticated %}

If so, we write, Welcome

We follow this up seeing if the user has a first name on file. If so, we say, Welcome followed by the first name and !

If not, we say, Welcome username!

We then insert a logout link through the line, <a href="{% url 'auth_logout' %}">Log Out</a>

If a user is not logged in, we write, <h3>Welcome! Would you like to <a href=" {% url 'auth_login' %}">log in?</a></h3>

This creates a log in button for when no one is logged in.

We then have the line that always prints, This is the Blog page.


And this is all that is needed to create dynamic username registration and login with Django.

django-registration-redux makes this process very easy.




HTML Comment Box is loading comments...