When I started web development with Django. I thoroughly didn't know about the django-allauth package. I had knowledge of MVC and some core parts of Django. During college projects I had been working on a demo project and done user authentication and all manually. That time one of my seniors advised me to use django-allauth. I did some research and configured django-allauth with my project within a couple of minutes. For me it was an invigorating thing.
Because it saves lots of time for registration, login, forget & reset passwords and user management. Also you can integrate a wide range of third party social authentication like Google, Facebook, LinkedIn, tumblr etc.
Here, I'll show you how simple it is to integrate django-allauth with your Django project.
First thing, You need to setup a virtual environment using virtualenv
and activate it, so all new python package installation goes to that environment instead of global installation.
Install and configure your basic Django application once you've completed the virtual environment setup.
After that, install latest django-allauth package the from Python Package Index(pip) in your virtual environment.
pip install django-allauth
It's ready to use in our project after a successful installation.
Configure allauth
module in a INSTALLED_APPS
in settings.py so INSTALLED_APPS
will be look like,
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# Add following thing on your code
'django.contrib.sites',
'allauth',
'allauth.account',
'allauth.socialaccount',
# For extra provider i will write another post
]
Add allauth back-end and site_id
at end or anywhere in settings.py.
AUTHENTICATION_BACKENDS = [
# it required to login by username in django admin panel
'django.contrib.auth.backends.ModelBackend',
'allauth.account.auth_backends.AuthenticationBackend',
]
# SITE_ID FROM django site framework
SITE_ID = 1
# redirect url after successful login
LOGIN_REDIRECT_URL = 'index'
You will get information of SITE_ID
from The βsitesβ framework.
If you have no idea, What is site_id? Just go to the admin panel by creating a superuser. And go to the following URL http://localhost:8000/admin/sites/site/ (top level domain might be different in your case). probably it would be 1
If you have not deleted else add a new one with the same domain.
Now if you try to run your project you may see some pending migrations files on your project.
You can migrate it by running following management command therefore all all-auth related tables will be created.
~$ python3 manage.py migrate
Once all thing finished in settings.py
, you need to setup all allauth related urls in project's urls.py
from django.contrib import admin
from django.urls import path
from django.urls import include #added to include allauth urls
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('src.core.urls')), # other apps URL
# allauth urls
path('accounts/', include('allauth.urls')),
]
I added accounts/ suffix for allauth urls based on documentation. However, you are free to use any related name that meets the needs of your project.
Now your project is ready to test using django all-auth.
just hit the url http://localhost:8000/accounts/signup/
from browser and it will show you built-in basic registration form like this
At http://localhost:8000/accounts/
will display all urls which are available under our allauth apps while debug=True
We can edit all the templates(HTML files) that are available under allauth by overriding into our template directory that template customization & social media integration will cover on upcoming post.
That's it for this post.π
Comments are welcome and appreciated!
Top comments (0)