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

后端 未结 12 2113
后悔当初
后悔当初 2020-12-07 15:05

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 mess

相关标签:
12条回答
  • 2020-12-07 15:06

    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.

    0 讨论(0)
  • 2020-12-07 15:09

    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.

    0 讨论(0)
  • 2020-12-07 15:10

    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"
            }
        }
    ]
    
    0 讨论(0)
  • 2020-12-07 15:10

    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

    0 讨论(0)
  • 2020-12-07 15:11

    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)

    0 讨论(0)
  • 2020-12-07 15:17

    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.

    0 讨论(0)
提交回复
热议问题