Django Login Page Example

As a web developer, I am excited to show you an example of a Django login page. Django is an impressive and user-friendly framework for creating web applications. The login feature is vital for numerous web applications, as it enables users to verify their identities and access customized content.

Before diving into the example, let’s briefly talk about Django. Django is a high-level Python web framework that follows the model-view-controller (MVC) architectural pattern. It provides a robust set of tools and features, including authentication and user management, making it an excellent choice for building secure and scalable web applications.

Setting Up the Project

First and foremost, let’s make sure we have Django installed on our system. Open your terminal and run the following command:

pip install django

Once Django is installed, let’s create a new Django project. In your terminal, navigate to the desired directory and run:

django-admin startproject loginpage

This command will create a new directory called ‘loginpage’ with the necessary files and folders for our project. Change into the project directory by running:

cd loginpage

Next, we need to create a new Django app within our project. Apps are individual components of a Django project that handle specific functionality. In your terminal, run:

python manage.py startapp accounts

This command will create a new directory called ‘accounts’ within our project. Now, let’s add this app to the list of installed apps in our project’s settings. Open the ‘settings.py’ file located in the ‘loginpage’ directory and add ‘accounts’ to the ‘INSTALLED_APPS’ list:


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

Creating the Login View

Now that we have our project and app set up, let’s start building our login page. In the ‘accounts’ app directory, create a new file called ‘views.py’. This file will contain our view functions.

Inside the ‘views.py’ file, add the following code:


from django.shortcuts import render

def login(request):
return render(request, 'accounts/login.html')

The ‘login’ function is a view function that takes a request object as a parameter and returns a rendered HTML template. In this case, we are rendering a template called ‘login.html’, which we will create next.

Now, let’s create the ‘login.html’ template. In the ‘accounts’ app directory, create a new folder called ‘templates’, and inside it, create another folder called ‘accounts’. Finally, create a new file called ‘login.html’ inside the ‘accounts’ folder.

Inside the ‘login.html’ file, add the following code:


<h2>Login</h2>

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

In this template, we have a simple HTML form with input fields for the username and password. The form’s ‘action’ attribute is set to the ‘login’ URL, which will be handled by our ‘login’ view function. We also include a CSRF token for security purposes.

Wiring Up the URLs

Now that we have our view and template, let’s wire up the URLs so that our login page can be accessed. In the ‘accounts’ app directory, create a new file called ‘urls.py’.

Inside the ‘urls.py’ file, add the following code:


from django.urls import path
from . import views

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

This code defines a URL pattern for the ‘login’ URL, which will be handled by our ‘login’ view function. The ‘name’ parameter is used to provide a unique identifier for this URL, which can be used to reference it in other parts of our code.

Testing the Login Page

Now that we have everything set up, let’s test our login page. In your terminal, run the following command to start the Django development server:

python manage.py runserver

With the server running, open your web browser and navigate to ‘http://127.0.0.1:8000/login/’. You should see the login page rendered with the form we created earlier.

Try entering a username and password and click the ‘Login’ button. For this example, we have not implemented any authentication logic, so the login process will not actually authenticate the user. However, you can extend this example by adding authentication and authorization functionality to your Django project.

Conclusion

Building a login page using Django is relatively straightforward and can be customized to fit the specific requirements of your web application. Django provides powerful authentication and user management features out of the box, making it an excellent choice for building secure and scalable web applications.

In this article, we walked through an example of creating a Django login page from scratch. We set up the project, created a login view and template, wired up the URLs, and tested the login page. With this foundation, you can now expand and enhance the login functionality to suit your application’s needs.

If you’re interested in learning more about Django and its features, I highly recommend checking out the official Django documentation. Happy coding!