Beginner guide for django-allauth
Tags: django, python, programming, djangoallauth
Published: 2022-05-22 14:55:59

When I started web development with Django. I thoroughly didn't know about the [django-allauth](https://github.com/pennersr/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](https://github.com/pennersr/django-allauth) package the from Python Package Index(pip) in your virtual environment.
```shell
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,
```python
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**.
```python
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](https://docs.djangoproject.com/en/3.1/ref/contrib/sites/).
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.
```shell
~$ python3 manage.py migrate
```

Once all thing finished in `settings.py`, you need to setup all **allauth** related urls in project's `urls.py`
```python
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](https://django-allauth.readthedocs.io/en/latest/). 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 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/31oqzkbg13t2jswrv4f2.png)
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!