Django-allauth is a powerful Django package that simplifiesuser authentication,registration,account management, andintegrationwith social platforms likeGoogle,Facebook, etc. It builds on Django’s built-in authentication system, providing a full suite of ready-to-use views and forms.
Prerequisite:Django-allauth setup and Configuration
Features of Django allauth:
- Allow users to sign up and log in using their email or username.
- Managelogin/logoutand email verification workflows.
- Add custom fields or logic to the registration process.
- Enable social logins with minimal configuration.
In this tutorial, we will set up a basic Django application that demonstrates how to use django-allauth for user authentication- covering login, logout, and signup functionality without using any custom templates.
Step 1: Create a Django Project and App
Create a new project and app, to learn about how to create and set it up, refer to:
Creating Django Project
Creating Django App
Suppose we created a project namedformand an app inside it, namedformapp.
Register formapp insettings.py so Django knows to include it in the project:
PythonINSTALLED_APPS=[...'formapp',# formapp registered here]
Step 2: Install and Configure django-allauth
We installdjango-allauth to extend Django's authentication system with features likeregistration,login,logout,email verification, andsociallogin. It providesprebuilt viewsandformsso we don't have to implement them manually.
Install it using the command:
pip install django-allauth
Add required apps to INSTALLED_APPS
We now add required apps that power the core features ofdjango-allauth, including site management and social accounts.
PythonINSTALLED_APPS=[...'django.contrib.sites',# Required by allauth to manage sites'allauth',# Core allauth functionality'allauth.account',# User account management (signup/login)'allauth.socialaccount',# For social login (optional)'formapp',# Your app]
Configure SITE_ID
This is needed for Django’s sites framework. Allauth uses this to differentiate between multiple domains. Since we’re using one site, we set:
SITE_ID = 1
Set up authentication backends
We need to tell Django to use both its default backend and the one provided byallauth, add these codes insettings.py:
PythonAUTHENTICATION_BACKENDS=("django.contrib.auth.backends.ModelBackend",# Default auth"allauth.account.auth_backends.AuthenticationBackend",# Required by allauth)
Define redirect URLs after login/logout
These control where users are redirected after theylogin orlog out:
PythonLOGIN_REDIRECT_URL='/'# Redirect after successful loginACCOUNT_LOGOUT_REDIRECT_URL='/accounts/login/'# Redirect after logout
Customize account behavior
Here we define what fields are required and how authentication should behave:
PythonACCOUNT_EMAIL_REQUIRED=True# Email must be providedACCOUNT_USERNAME_REQUIRED=True# Username is also requiredACCOUNT_AUTHENTICATION_METHOD='username_email'# Users can log in using username or emailACCOUNT_EMAIL_VERIFICATION='none'# Skip email verification for simplicity
Ensure required middleware and context processors are present
allauthneeds access to the request object in templates. Add the following toTEMPLATES:
'django.template.context_processors.request',
And include theAccountMiddleware:
'allauth.account.middleware.AccountMiddleware',
Step 3: Create a Custom Signup Form
Sometimes you want to collect additional user data or change signup behavior. We do this by creating a form that extendsSignupForm.
Create the following insideformapp/forms.py (make sure it is inside the app folder, not the project folder):
Pythonfromallauth.account.formsimportSignupFormfromdjangoimportformsclassCustomSignupForm(SignupForm):first_name=forms.CharField(max_length=30,label='First Name')last_name=forms.CharField(max_length=30,label='Last Name')defsave(self,request):user=super().save(request)user.first_name=self.cleaned_data['first_name']user.last_name=self.cleaned_data['last_name']user.save()returnuser
Then, tell Django to use this form:
Python# settings.pyACCOUNT_FORMS={'signup':'formapp.forms.CustomSignupForm',}
This replaces the default signup form with your custom one.
Step 4: Set Up URLs
django-allauth provides ready-made views like/login/, /signup/, /logout/. To enable them, include its URLs in the project’surls.py:
Python# form/urls.pyfromdjango.contribimportadminfromdjango.urlsimportpath,includeurlpatterns=[path('admin/',admin.site.urls),path('accounts/',include('allauth.urls')),]
Now visiting/accounts/login/ or /accounts/signup/ will render the custom signup form (if defined), or the default form otherwise.
Step 5: Run the Server and Test
Migrate the database to create necessary tables:
python manage.py makemigrations
python manage.py migrate
Open your browser and visit:
- Toregistera new user:http://127.0.0.1:8000/accounts/signup/
- Tolog in: http://127.0.0.1:8000/accounts/login/
- Tolog out:http://127.0.0.1:8000/accounts/logout/
Outputs:
Snapshot of the sign-up page
Snapshot of the sign-in pageRead Next Article:Django Sign Up and login with confirmation Email