[Error Fixed] How to Get SITE_ID for Allauth Authentication in Django?
If you are using
allauth Django authentication library, it is necessary to set the SITE_ID in the Django project setting.
I was using this library to set the Django social login authentication in one of my Python projects. I read many of the tutorials on the internet to get the site ID. Almost all the tutorials were claiming to try random numbers.
It’s cumbersome to change the site ID in the setting and run the application again. If it fails try with a new site ID and repeat the process again. This is really bullshit. Don’t do it.
Further, if you give the wrong SITE_ID for
allauth and execute Django
runserver. You will get an error saying…
Error : `SocialApp matching query does not exist.
Without fixing this error, you can implement authentication.
Here is the hack to get SITE_ID for
allauth and to solve this problem.
Method 1: Using Django Admin
Follow the steps below to get the site ID.
- Login into the Django administrations dashboard. (Use your superuser account to login).
If you are using localhost, open url
127.0.0.1:8000/admin/in your browser and login.
- Select Sites from SITES in the left sidebar.
- Select desired site. Here I’m choosing
- Check the URL, you will see the site ID.
- You can use the same dashboard to add a new site for authentication.
- By default, you will also see the
example.comas a site listed in the sites. You can simply remove it.
Method 2: Using Django Shell
You can also use the Django interactive shell to get the SITE_ID for any website. Couple of commands you need to execute.
# python manage.py shell Python 3.6.9 (default, Dec 8 2021, 21:08:43) [GCC 8.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> from django.contrib.sites.models import Site >>> new_site = Site.objects.create(domain='127.0.0.1:8000', name='127.0.0.1:8000') >>> print(new_site.id) 3
Just replace the domain and name for the desired site.
Here the id for the mentioned site is 3.
This is a simple and very useful hack to get the SITE_ID for
allauth for any website that uses
allauth Django authentication library.