39

Im trying to get the admin site of my app in Django working. Ive just sync`d the DB and then gone to the site but I get the error ...

Site matching query does not exist.

Any ideas ?

karthikr
  • 97,368
  • 26
  • 197
  • 188
felix001
  • 15,341
  • 32
  • 94
  • 121
  • 1
    It's a duplicated question http://stackoverflow.com/questions/11476210/getting-site-matching-query-does-not-exist-error-after-creating-django-admin – Fernando Freitas Alves Apr 17 '13 at 19:28
  • Possible duplicate of [Getting Site Matching Query Does Not Exist Error after creating django admin](https://stackoverflow.com/questions/11476210/getting-site-matching-query-does-not-exist-error-after-creating-django-admin) – Aniket Thakur Feb 06 '18 at 07:24
  • 1
    I removed `example.com` site in `django_site` table and added new domain. So I had same issue with you. I changed value of `SITE_ID` to `2` in `settings.py` file. It's for new site. ``` SITE_ID=2 ``` – NinjaDev Sep 03 '20 at 03:33

7 Answers7

57

Every django app needs a Site to run. Here you do not seem to have it.

Log into your django shell

$> ./manage.py shell
>>> from django.contrib.sites.models import Site
>>> site = Site()
>>> site.domain = 'example.com'
>>> site.name = 'example.com'
>>> site.save()

or

$> ./manage.py shell
>>> from django.contrib.sites.models import Site
>>> site = Site.objects.create(domain='example.com', name='example.com')
>>> site.save()

You should be all set.

karthikr
  • 97,368
  • 26
  • 197
  • 188
42

Add django.contrib.sites in django INSTALLED_APPS and also add SITE_ID=1 in your django setting file.

CoderGuy123
  • 6,219
  • 5
  • 59
  • 89
Mr Singh
  • 3,936
  • 5
  • 41
  • 60
23

Adding SITE_ID=1 on settings.py did the trick for me.

9

You also need to make sure that the site domain is the same with the one you actually use. For example if you are you are accessing the admin site from http://127.0.0.1:8000/admin/ then your site.domain should be: site.domain = '127.0.0.1:8000'.

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
5

Try out this in settings.py:

SITE_ID = 1
3

SITE_ID = 1 i didn't work for me

I had to go to the python shell ./manage.py shell

Then get the existing site id using name or domain

from django.contrib.sites.models import Site
Site.objects.get(name='back').id
> 6

For me it was 6, all i had to do then is to change the SITE_ID = 6 and it worked fine

Ameur Baccoucha
  • 559
  • 1
  • 8
  • 17
0

I'm using wsgi, so I had to reboot twice, not sure why.

then clear cache and that was it.

Brian Sanchez
  • 832
  • 1
  • 13
  • 11