Create Login Page Django

Making a login page in Django is an essential aspect of any online platform. It enables individuals to safely enter restricted areas and view personalized information. In this article, I will provide a step-by-step guide on how to create a login page using Django framework, while also sharing my personal insights and helpful advice.

Setting Up Django

Before we dive into creating the login page, make sure you have Django installed. If not, open your terminal and run the following command:

pip install Django

Once Django is installed, create a new Django project by running the command:

django-admin startproject myproject

Navigate into the project directory:

cd myproject

Creating the Login App

Now, let’s create a new Django app specifically for handling login functionalities. In your terminal, run:

python manage.py startapp loginapp

This will create a new directory named ‘loginapp’ inside your Django project.

Next, we need to configure our project’s settings to include the newly created app. Open the ‘settings.py’ file in your project’s directory and add ‘loginapp’ to the ‘INSTALLED_APPS’ list:

'loginapp',

Designing the Login Page

Now that our app is set up, let’s create the login page’s HTML template. Inside the ‘loginapp’ directory, create a new directory named ‘templates’. Within the ‘templates’ directory, create another directory named ‘loginapp’.

Inside the ‘loginapp’ directory, create a new file called ‘login.html’. This file will contain the HTML code for our login page. Feel free to add your own personal touches to the design of the login page.

Here’s a simple example of how the ‘login.html’ file could look:


<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<form method="POST" action="{% url 'login' %}">
{% csrf_token %}
<input type="text" name="username" placeholder="Username"><br>
<input type="password" name="password" placeholder="Password"><br>
<input type="submit" value="Login">
</form>
</body>
</html>

Don’t forget to add the necessary Django template tags to handle form submission and cross-site request forgery protection.

Creating the Login View

Now, let’s create the login view to handle the form submission and authentication process. Inside the ‘loginapp’ directory, open the ‘views.py’ file and add the following code:


from django.contrib.auth import authenticate, login
from django.shortcuts import render, redirect

def login_view(request):
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('home')
return render(request, 'loginapp/login.html')

This view will handle the login logic. If the form is submitted via POST method, it will authenticate the user and redirect them to the ‘home’ page if the credentials are correct. Otherwise, it will render the login page again.

Creating the Login URL

Lastly, we need to define the URL pattern for the login page in the ‘urls.py’ file. Open the ‘urls.py’ file in your project’s directory and add the following code:


from django.urls import path
from loginapp import views

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

Now, if you navigate to ‘http://localhost:8000/login/’, you should see your login page! Try entering your credentials and see if the authentication process works as expected.

Conclusion

Creating a login page in Django is an essential step in building secure and user-friendly web applications. By following the steps outlined in this article, you should now have a functioning login page that integrates with Django’s authentication system.

Remember to style your login page to match the overall design of your application and consider adding additional features like password reset and registration functionalities to enhance the user experience.

Feel free to explore Django’s documentation for more advanced login page customization options and security best practices.

Go ahead, take your Django web application to the next level with a well-designed and secure login page!