How To Make Login Page In Django

Creating a login page is a crucial step in developing a secure and user-friendly website. In this article, I’ll guide you through the process of making a login page in Django, a powerful Python web framework. With Django’s built-in authentication system, you can quickly set up a robust login page that meets your specific requirements.

Why Django?

Django provides a comprehensive authentication system that handles user registration, login, and logout. This built-in functionality saves you time and effort, allowing you to focus on other aspects of your web application. Additionally, Django’s secure and flexible design ensures that your login page is protected against common vulnerabilities, such as cross-site scripting (XSS) and cross-site request forgery (CSRF).

Step 1: Installing Django

Before we dive into creating the login page, you’ll need to have Django installed on your system. If you haven’t already, open your terminal and run the following command:

pip install django

Step 2: Setting Up a Django Project

Once Django is installed, let’s create a new Django project. In your terminal, navigate to the directory where you want to create your project and run the following command:

django-admin startproject myproject

This will create a new directory called ‘myproject’ with the necessary files and directories for your Django project.

Step 3: Creating a Django App

In Django, functionality is organized into apps. Let’s create a new app called ‘accounts’ that will handle our authentication features. In your terminal, navigate to the root directory of your Django project and run the following command:

python manage.py startapp accounts

This will create a new directory called ‘accounts’ within your project, containing the necessary files for the app.

Step 4: Configuring the Django Settings

Next, we need to configure our Django project to include the ‘accounts’ app and set up the authentication backend. Open the ‘settings.py’ file within your project’s directory and update the ‘INSTALLED_APPS’ and ‘AUTHENTICATION_BACKENDS’ sections as follows:


INSTALLED_APPS = [
...
'accounts',
...
]

AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend',
]

By adding ‘accounts’ to the ‘INSTALLED_APPS’ list, Django will recognize our ‘accounts’ app and include its functionality in our project. The ‘AUTHENTICATION_BACKENDS’ list specifies the backend that Django should use for authentication, which in this case is the default ‘ModelBackend’.

Step 5: Creating the Login View

Now it’s time to create the login view. Open the ‘views.py’ file within the ‘accounts’ app directory and add the following code:


from django.shortcuts import render
from django.contrib.auth.decorators import login_required

@login_required
def home(request):
return render(request, 'accounts/home.html')

In this code, we import the necessary modules and use the ‘login_required’ decorator to ensure that only authenticated users can access the ‘home’ view. You can customize this view to suit your needs, such as redirecting users to their profile page or a dashboard.

Step 6: Creating the Login Template

To create the login page, we need to create a template. Within the ‘accounts’ app directory, create a new directory called ‘templates’. Inside the ‘templates’ directory, create another directory called ‘accounts’. Finally, create a new file called ‘login.html’ inside the ‘accounts’ directory.

In the ‘login.html’ file, you can add your HTML and CSS code to create the login form. Here’s a simple example:


<form method="POST" action="{% url 'login' %}">
{% csrf_token %}
<label for="username">Username:</label>
<input type="text" id="username" name="username">
<label for="password">Password:</label>
<input type="password" id="password" name="password">
<input type="submit" value="Login">
</form>

In this code, we create a form with fields for username and password. The ‘action’ attribute of the form specifies the URL to which the form should be submitted, and the {% csrf_token %} tag ensures CSRF protection.

Step 7: Creating the URL Pattern

Finally, we need to create a URL pattern that maps to our login view. Open the ‘urls.py’ file within the ‘accounts’ app directory and add the following code:


from django.urls import path
from . import views

urlpatterns = [
path('login/', views.home, name='login'),
]

In this code, we import the necessary modules and define a URL pattern for the ‘login/’ path, which will be handled by the ‘home’ view we created earlier.

Conclusion

By following these steps, you can create a login page in Django that provides authentication functionality for your web application. Django’s built-in authentication system makes it easy to implement secure login functionality, saving you time and effort. Remember to customize the views and templates to fit your specific requirements. Happy coding!