62

I'm going through the standard Django tutorial to create an admin for an app. After commenting the admin related stuff in settings and running syncdb I'm getting this message:

DoesNotExist at /admin/ Site matching query does not exist.

Can anyone help me figure this out?

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
David Haddad
  • 3,796
  • 8
  • 32
  • 40

13 Answers13

108

The Site object for your Django project is missing. Each Django project has a Site object which contains the site's name and domain. It is usually automatically created when creating a Django project (in particular, when the syncdb command runs) but in your case it seems that didn't happen.

To fix it:

Open the Django shell for your site (python manage.py shell).

Type the following:

>>> from django.contrib.sites.models import Site
>>> Site.objects.create(name='example.com', domain='example.com')

If you want to change these values later, go to your admin panel (/admin/) and edit the site object in the section Sites.

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
  • 2
    thanks so much. just to clarify at which point in creating your site should the site object be created? When you do: django-admin.py startproject mysite? – David Haddad Jul 13 '12 at 18:49
  • @DavidHaddad: I just checked the Django source and it happens after the `syncdb` command has created the tables. If your project then uses the `django.contrib.sites` addon then it'll run some code to create the `Site` object. – Simeon Visser Jul 13 '12 at 18:59
  • Sweet, it's exactly after running syncdb that the error was raised. Thanks again. – David Haddad Jul 13 '12 at 19:15
  • 2
    Have to also change the SITE_ID in settings.py – Engineer2021 Feb 03 '14 at 16:34
  • I still got the error after I created the site object with name "example.com". Please help – wrufesh Oct 30 '15 at 09:01
  • As Brian mentioned [along with this answer](https://stackoverflow.com/a/9737344/943773), setting `SITE_ID=1` did the trick for me. You can also just remove `sites` from `INSTALLED_APPS` as mentioned the answer I linked. – ryanjdillon Jan 22 '18 at 17:18
45

In addition to Simeon Visser's answer for those of you still experiencing problems, make sure the SITE_ID variable in your settings matches the ID of your newly created Site-object.

Milo Wielondek
  • 4,164
  • 3
  • 33
  • 45
  • 2
    Definitely worth noting that with django 1.8, migrating after adding the `contrib.sites` does add a site, but you must add the `SITE_ID` setting. Simeon's answer isn't even required - just the `SITE_ID` to 1. – Yuji 'Tomita' Tomita Sep 25 '15 at 05:55
  • 2
    if you screw it up like I just did with deleting the record with SITE_ID 1, just create the new record with id=1 in the shell. – ger.s.brett Mar 02 '16 at 11:19
  • i spent about 30 minutes trying to debug this..turns out i had deleted the initiated site provided by default in django 'example.com' anf therefore my site id was not 1 – Brian Obot Jun 30 '22 at 11:27
37

When you include the django.contrib.sites to your INSTALLED_APPS and run the command "python manage.py migrate" the app automatically creates a object into "django_site" table (with domain name and display name equals to "example.com". There is no need to create it by yourself.

Probably you just need to add the setting SITE_ID = 1 to your settings.py file.

Ricardo Silva
  • 815
  • 10
  • 12
10

If you already have example.com in your sites table after you run

python manage.py migrate

You need to get id of this entry.

To get the ID you can do -

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
  • 66,731
  • 38
  • 279
  • 289
7

comment out django.contrib.sites from the installed apps. i.e.

#'django.contrib.sites',
  • This fixed my issue as I had been playing with my settings trying to get a package to work. – MPowerKC Apr 06 '17 at 18:34
  • Yup. +1. Although not necessarily a fix for people needing sites; this is very helpful for diagnosing where the problem is. – thclark Nov 07 '17 at 11:30
4

You could also consider of using fixture feature of django to populate the data automatically: https://docs.djangoproject.com/en/dev/howto/initial-data/

[
    {
        "model" : "sites.site",
        "pk" : 1,
        "fields": {
            "name"  : "example.com",
            "domain" : "127.0.0.1:8010"
        }
    }
]
Miao ZhiCheng
  • 617
  • 7
  • 9
1

Site object is missed so you have to add 1 Site object

Solution:

open Django shell(python manage.py shell):

In [1]: from django.contrib.sites.models import Site

In [2]: Site.objects.create(name='example.com',domain='example.com').save()

In [3]: s=Site.objects.filter(name='example.com')[0]

In [4]: s.id
Out[4]: 3

then open your settings.py file and add SITE_ID = 3 put that value in SITE_ID = which you get after (s.id)

TheParam
  • 10,113
  • 4
  • 40
  • 51
0

I'm a Django newbie. I got the same error when going through the tutorial. My django_site DB table was empty. I chose to drop all tables named "django_*". Then when I reran syncdb, the missing django tables were created, and the django_site DB table was populated with id=1, domain=example.com, name=example.com. Evidently the Site class is backed by the django_site DB table. Now I understand the problem and the solution above that populated the table using the Site.objects.create() method. Thanks Simeon.

Phil Friesen
  • 121
  • 1
  • 2
0

If you use South and initial_data fixtures, data could be loaded not properly. To fix it add

if 'test' in sys.argv or 'jenkins' in sys.argv:
    SOUTH_TESTS_MIGRATE = False

at the end of your settings file

Thorin Schiffer
  • 2,818
  • 4
  • 25
  • 34
0

Sometimes if you add more Sites via Admin and delete some of them each site has an ID, it was not working for me once I changed the ID in the DB it started working, so make sure SITE_ID matches the ID in the database.

Radek
  • 1,149
  • 2
  • 19
  • 40
0

I was also struggling with the same error for quite some time now. I had by mistake deleted the example.com from sites directory in admin. Once I added the site using above solutions, it worked. Also site_id automatically became 2 when I created the example.com and so had to change site_id to 2 in my settings file also. Thank you.

0

Its all due to if you accidentally delete site from django admin site which is by default (example.com). The code mentioned below also raised an Runtime error.

from django.contrib.sites.models import Site

so simply make an increment in your SITE_ID by 1 For example if by default it is 1 than change it with 2 change from SITE_ID = 1 TO: SITE_ID = 2

Mubashar Javed
  • 1,197
  • 1
  • 9
  • 17
0
from django.contrib.sites.models import Site
Site.objects.create(name='example.com', domain='example.com')

Above solved the issue.

But when I logged in Admin Panel, there was another site example.com so I believe if someone puts SITE_ID = 2 this will also work.

Josef
  • 2,869
  • 2
  • 22
  • 23