Django provides a built-in authentication system that makes it easy to implement user login, logout, and sign-up functionality in your web application. In this tutorial, we’ll walk you through the steps to use Django’s built-in authentication system to create a login, logout, and sign-up functionality in your web app.
Prerequisites
Before we start, you should have a basic understanding of:
- Django web framework
- HTML, CSS, and JavaScript
Step 1: Create a Django Project
Create a new Django project by running the following command in your terminal:
django-admin startproject myproject
Step 2: Create a Django App
Create a new app in your Django project by running the following command:
python manage.py startapp accounts
Step 3: Configure URL patterns
In your myproject/urls.py
file, include the URL pattern for your accounts app by adding the following code:
from django.urls import include, path
urlpatterns = [
path('accounts/', include('accounts.urls')),
# ...
]
Step 4: Create URL patterns for the accounts app
In your accounts/urls.py
file, add the following URL patterns:
from django.urls import path
from django.contrib.auth import views as auth_views
from . import views
urlpatterns = [
path('login/', auth_views.LoginView.as_view(template_name='accounts/login.html'), name='login'),
path('logout/', auth_views.LogoutView.as_view(), name='logout'),
path('signup/', views.signup, name='signup'),
]
These patterns will map to the built-in views for login and logout, and a custom view for the sign-up page.
Step 5: Create Login, Logout, and Sign-up Templates
Create three templates for login, logout, and sign-up by adding the following HTML code in the corresponding templates/accounts/
directory:
login.html
{% extends 'base.html' %}
{% block content %}
<h2>Login</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Log in</button>
</form>
{% endblock %}
logout.html
{% extends 'base.html' %}
{% block content %}
<h2>Logged out</h2>
{% endblock %}
signup.html
{% extends 'base.html' %}
{% block content %}
<h2>Sign up</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Sign up</button>
</form>
{% endblock %}
Step 6: Create Sign-up View
Create a view for sign-up by adding the following code in the accounts/views.py
file:
from django.shortcuts import render, redirect
from django.contrib.auth.forms import UserCreationForm
def signup(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
return redirect('login')
else:
form = UserCreationForm()
return render(request, 'accounts/signup.html', {'form': form})
This view uses the UserCreationForm
to create a new user account
Step 7: Add Authentication URLs to Settings
Add the following lines to your myproject/settings.py
file:
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/'
These lines will redirect users to the homepage after login or logout.
Step 8: Include Authentication Forms in Templates
To display the authentication forms in your templates, add the following code to your base.html
template:
{% if user.is_authenticated %}
<a href="{% url 'logout' %}">Log out</a>
{% else %}
<a href="{% url 'login' %}">Log in</a> /
<a href="{% url 'signup' %}">Sign up</a>
{% endif %}
This code will display “Log out” if the user is authenticated and “Log in / Sign up” if not.
Step 9: Run Migrations and Start the Server
Run the migrations by running the following command in your terminal:
python manage.py migrate
Then start the server:
python manage.py runserver
You should now be able to navigate to http://localhost:8000/accounts/login/
to see the login page. You can also navigate to http://localhost:8000/accounts/signup/
to see the sign-up page.
Conclusion
In this tutorial, we covered the steps to implement a user authentication system using Django’s built-in authentication system. By following these steps, you should now have a working login, logout, and sign-up functionality in your Django web application.
🎉 Thanks for reading
Hey there — I hope you found this article informative and helpful. If you have any questions or comments, please feel free to reach out to me via email (alisheikh1114@gmail.com). I am always eager to connect with readers and help them with any technical challenges they may be facing.
👨💻 Let’s connect on social media
Don’t forget to follow me on Medium if you are interested in reading about Web Dev Stacks, Big Data, and Open Source.
Find me online: GitHub • LinkedIn • Twitter • Facebook • Instagram
🤝 Share the knowledge
If you found this article useful, please consider sharing it with your friends and colleagues who might benefit from it. Sharing is caring, and it helps spread knowledge and insights to those who need it the most.