To create a signup, login, logout, and authentication app with Django, you’ll need to follow these steps:
Install Django and create a new project:
pip install django
django-admin startproject auth_app
Create a new app within the project:
python manage.py startapp accounts
Create a model for storing user accounts in accounts/models.py
. You can use the AbstractUser
model provided by Django as a base, or create your own model with the fields you need:
from django.contrib.auth.models import AbstractUser
from django.db import models
class User(AbstractUser):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
email = models.EmailField(unique=True)
Run the database migrations to create the database table for the user accounts:
python manage.py makemigrations
python manage.py migrate
Create a form for the signup and login pages in accounts/forms.py. You can use the UserCreationForm and AuthenticationForm provided by Django, or create your own forms with the fields you need:
from django import forms
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from .models import User
class SignupForm(UserCreationForm):
class Meta:
model = User
fields = ['username', 'email', 'password1', 'password2']
class LoginForm(AuthenticationForm):
pass
Create view functions for the signup, login, and logout pages in accounts/views.py. You can use the login and logout functions provided by Django, or create your own views:
from django.shortcuts import render, redirect
from django.contrib.auth import login, logout
from .forms import SignupForm, LoginForm
def signup(request):
if request.method == 'POST':
form = SignupForm(request.POST)
if form.is_valid():
user = form.save()
login(request, user)
return redirect('home')
else:
form = SignupForm()
context = {'form': form}
return render(request, 'accounts/signup.html', context)
def login(request):
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
user = form.get_user()
login(request, user)
return redirect('home')
else:
form = LoginForm()
context = {'form': form}
return render(request, 'accounts/login.html', context)
def logout(request):
logout(request)
return redirect('home')
These view functions handle the POST requests from the signup, login, and logout forms and use the login
and logout
functions provided by Django to log the user in and out.
Create templates for the signup, login, and logout pages in the accounts/templates/accounts
directory. You can use the following code as a starting point:
Sign Up
# login.html
Log In
#logout.html
Log Out
These templates define the forms for the signup, login, and logout pages, respectively.
Follow Me on Linkedin & medium.com for further updates and problem-solving blogs
Author: Muhammad Sameer
I am a talented, ambitious, and hardworking individual, with broad skills and experience in digital and printed marketing, social media, and leading projects. Furthermore, I am adept at handling multiple tasks on a daily basis competently and at working well under pressure. A key strength is communication; building strong relationships with people in order to deliver the best results. Recently, I completed some projects, including Business and Design modules as a task. I am able to handle multiple tasks on a daily basis. I use a creative approach to problem-solving. I am a dependable person who is great at time management. I am always energetic and eager to learn new skills. I have experience working as part of a team and individually also have an experience in different technology