Getting Site Matching Query Does Not Exist Error after creating django admin

核能气质少年 提交于 2019-11-28 03:32:48

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.

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.

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.

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

#'django.contrib.sites',

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"
        }
    }
]

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.

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.

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

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.

Rahul Singh

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)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!