Django login system

In our previous Django tutorial, we learned how to install this web framework on your Windows devices and create our first Django project by simply running a few commands. As you might know by now, Django provides programmers with rapid development features, and thus, you won’t have to spend your precious time reinventing the wheels. As such, this web framework also comes with its own built-in login system that we’ll teach you how to use in this article.

Using Django’s Login System

Before we begin, we’d like to mention that we’ll be using Sublime Text and PyCharm for this tutorial, so make sure to install them if you already haven’t. Plus, you can follow this tutorial regardless of whether you’re coding on Windows or Linux. Now getting to the built-in authentication system of Django, you will be getting a bunch of out-of-the-box functionalities, such as User Registration, Login, Logout, Change Password, and Reset Password.

Step 1 – Create superuser

We’re assuming that you’ve already created your first Django project in our previous tutorial. If you haven’t, go do it right now since you can’t create the superuser without a project in the first place.

Apart from that, you would also have to make sure that your virtual environment is activated so that you can run Django commands. If it’s not, you have to open your command prompt, navigate to your project folder, and run the following command:

first-project\Scripts\activate.bat

Then, get to the testsite directory and run the following command for making first-time migrations:

python manage.py migrate
Making Migrations in Django

Once you’re done with that, you’re all set to create the superuser. Running the following command should do:

python manage.py createsuperuser

After you run this command, you will be asked to enter your username, email, and password. With this, you’ve just created the superuser for your Django project.

Creating superuser

Step 2 – Add URL routes

Once the superuser is created, you have to add the login and logout URL routes to your ‘urls.py’ file that you can find in your web app’s main directory. Here, open this file using PyCharm and add the following code to import Django’s built-in authentication views:

from django.contrib.auth import views as auth_views

What you have to do now is add the login and logout URL patterns to your code as well:

path('login/', auth_views.LoginView.as_view(), name='login'), 
path('logout/', auth_views.LogoutView.as_view(), name='logout'),
Adding URL routes

With this, the login and logout views should be activated once you visit http://localhost:8000/login or http://localhost:8000/logout, respectively.

Step 3 – Create template

Although the login and logout views are in place now, there’s currently no way to activate them by using the front-end of the website. For this, we will create the login template here in this step.

Before we begin, you should note that we’re not going to be creating a full-fledged login template but only a minimal one. So, the first thing that you’d have to do is get to the main project directory. From there, open the project folder (which in our case, is ‘first-project) and navigate to Lib > site-packages > django > contrib > admin > templates > registration. Now, within that folder, create a new file with the name of ‘login.html’. If you’re having trouble with this step, let’s have a look at how our directory tree looks like:

Directory Tree

Now you can open this HTML file using Sublime Text. Next, you should enter the following code for taking in the login credentials of the user:

{% block content %}
  <h3>Login</h3>
  <form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Login</button>
  </form>
{% endblock %}
Login Page

Step 4 – Redirect Login

With this, the login page should be ready. However, once you’ve entered your correct credentials and hit the Login button, the website should take you to the profile page for that user. However, it is also possible for you to redirect the website to any other page you want. Here, we’ll be redirecting it to our homepage, assuming that you have one built already. So, in the settings.py file of your project, you need to add the following line of code:

LOGIN_REDIRECT_URL = 'home'

Note that ‘home’ is the name of the path saved in the ‘urls.py’ file rather than a template name.

Step 5 – Redirect Logout

Similar to the Login procedure, you will be redirected to a default page once you’ve successfully logged out of your website. So, if you want to take the user to a different page, you can do so by adding the following line of code in the same settings.py file that you’ve opened in the previous step:

LOGOUT_REDIRECT_URL = 'home'

As in the previous step, you can enter any URL name in place of the ‘home’ we’ve gone with. With this, you’ve successfully integrated the login and logout functionalities in your Django web application.

Conclusion

It’s no secret that Django gives you several out-of-the-box functionalities that require only a few lines of code. We’re sure this tutorial gave you a glimpse of what Django’s built-in authentication system is all about. However, apart from login and logout, Django also helps you with user registration and password reset functionalities, and they’re just as easy to implement. Lastly, if this tutorial was of any help to you, please let us know in the comment section below.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *