Creating a Login functionality - Django

Updated on Oct. 8, 2017 | 199 |   django,

In this tutorial, we are going to create a user login form and going to use django built-in login authentication system to make the login form functioning. Let's start!

Step 1: Let’s create the login form first. 

  <form method="POST">

  {% csrf_token %}

  <p><label for="id_username">Username:</label> 

  <input type="text" name="username" autofocus maxlength="254" required/></p>

  <p><label for="id_password">Password:</label> 

  <input type="password" name="password" required/></p>

  <input type="submit" value="Login">


Make sure, you’re NOT changing the name attributes of username and password input fields. For, CSRF protection, we have added {% csrf_token %}. Method must be in POST mode.

Step 2: In, add a URL for telling Django which page you want to be redirected after login. 

   LOGIN_REDIRECT_URL = '/home/'

Step 3: In, add the lines below.

   from django.contrib.auth.views import login

   urlpatterns = [

           url(r'^login/$', login, {'template_name':'main/login.html'}),


Here, we have first imported the login module from Django built-in authentication system. Inside url(), first argument expresses our expected login URL. Then in second argument, we have given our login view, which we imported from Django built-in authentication. Finally, in third argument, we have told which template should have our login form. 

That’s it. Your login page is done!