[Django]-Site matching query does not exist

167๐Ÿ‘

โœ…

If you donโ€™t have a site defined in your database and django wants to reference it, you will need to create one.

From a python manage.py shell :

from django.contrib.sites.models import Site
new_site = Site.objects.create(domain='foo.com', name='foo.com')
print (new_site.id)

Now set that site ID in your settings.py to SITE_ID

๐Ÿ‘คjdi

36๐Ÿ‘

Table django_site must contain a row with the same value than id (by default equals to 1), as SITE_ID is set to (inside your settings.py).

11๐Ÿ‘

Add SITE_ID = 1 to settings.py in your django project.

7๐Ÿ‘

I see answers to create a new site and reference id for that. But if you already have a site or somehow you deleted and created the site again from UI then the id keeps on incrementing. To solve this you can get the id as follows:

python manage.py shell
from django.contrib.sites.models import Site
print(Site.objects.get(name='example.com').id)

Get the id you get here into setting.py . Eg.

SITE_ID = 8

Basically ID in table corresponding yo tour site and in the settings.py should match.

๐Ÿ‘คAniket Thakur

5๐Ÿ‘

I fixed it without using python manage.py shell

At first I tried using the commands above using manage.py shell:

from django.contrib.sites.models import Site
new_site = Site.objects.create(domain='foo.com', name='foo.com')
print(new_site.id)

But it gave out an INTEGRITY ERROR

The short answer is add SITE_ID = 1 to your settings.py
If you want to know what your site id is, then go into the actual database, I downloaded sqliteman to see what my table had. So whatever site id you have in the table is what gets assigned to SITE_ID

This is because there is a function get_current that goes looking for SITE_ID and it doesnโ€™t find it in your settings.py

tables
-django_site
--Columns
---id

it should have id as 1, name as example.com, domain as example.com

๐Ÿ‘คRenato Espinoza

1๐Ÿ‘

enter image description here

Query the ID in your database django_site tables and set the right one in your Django settings.py, for example: SITE_ID = 3

๐Ÿ‘คFreman Zhang

1๐Ÿ‘

it seems you fotgot to add
SITE_ID=1
in settings.py

0๐Ÿ‘

I got the same error while I was creating the sitemap for the project. I included this 'django.contrib.sites' in the Installed_APP list in the setting.py file.
By removing the 'django.contrib.sites' from the installed_app list the error gets removed.

๐Ÿ‘คVinay Khatri

0๐Ÿ‘

ensure you add site id, below is a sample:

SITE_ID = 1

0๐Ÿ‘

Sites should be accessible via the admin panel once you add django.contrib.sites to your appโ€™s INSTALLED_APPS.

Once you do this, simply access: <your_django_app_admin_url>/sites/site/. From there, you can manage sites and see their IDs by accessing a site detail and looking at the browserโ€™s URL, which should look similar to <your_django_app_admin_url>/sites/site/<your_site_id>/change/.

๐Ÿ‘คErwol

Leave a comment