How To Make A Login Page In Django

Web Development Software

Creating a login page in Django is an essential part of building a web application that requires user authentication. In this article, I will guide you through the process of creating a login page in Django, adding personal touches and commentary from my own experiences. Let’s dive in!

Setting up Django

Before we begin, make sure you have Django installed on your machine. If not, you can install it using pip with the command:

pip install django

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

django-admin startproject myproject

Navigate into the project directory:

cd myproject

Now, let’s create a new Django app within our project:

python manage.py startapp myapp

Creating the Login Template

Next, we need to create a template for our login page. Inside the myapp directory, create a new directory called templates. Within the templates directory, create another directory called myapp.

Inside the myapp directory, create a new file called login.html. This is where we will define the structure and design of our login page.

Here’s an example of what the login.html file might look like:


<html>

<head>
<title>Login Page</title>
</head>

<body>
<h3>Welcome to the Login Page!</h3>

<form method="POST" action="{% url 'login' %}">
{% csrf_token %}
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br>

<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br>

<input type="submit" value="Login">
</form>
</body>

</html>

This is a basic login form with fields for the username and password, and a submit button to log in.

Handling the Login Form

Now that we have our login form, let’s handle the form submission in Django. Open the views.py file inside the myapp directory and add the following code:


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

def login_view(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('dashboard')
else:
return render(request, 'myapp/login.html', {'error': 'Invalid username or password'})
return render(request, 'myapp/login.html')

In this code, we first import the necessary functions from Django. Then, we define a function called login_view which handles the login form submission. If the request method is POST, we retrieve the username and password from the form data and use the authenticate function to verify the credentials. If the user is authenticated successfully, we log them in using the login function and redirect them to the dashboard page. If the credentials are invalid, we render the login.html template again with an error message.

Next, we need to create a URL pattern for the login view. Open the urls.py file inside the myproject directory and add the following code:


from django.urls import path
from myapp import views

urlpatterns = [
path('login/', views.login_view, name='login'),
# other URL patterns
]

Now, if you visit http://localhost:8000/login/ in your web browser, you should see the login page we created earlier.

Conclusion

Creating a login page in Django is a fundamental step in building secure web applications. In this article, we went through the process of setting up Django, creating a login template, handling the login form submission, and adding a URL pattern for the login view. By following these steps, you can easily create a login page in Django for your own projects.

Remember, user authentication is a crucial aspect of web development, as it ensures that only authorized users can access certain functionalities or data. As you continue to build your Django applications, make sure to implement additional security measures, such as password hashing and account verification, to further protect your users’ information.