3

Yesterday I set up a new Django app and the admin was still ok when I stopped working. This morning I run the server, and I see this error page:

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

I hadn't touched any of the code or made any changes to the database. While looking for an answer, I saw this post, and indeed, removing 'django.contrib.sites' from INSTALLED_APPS in my settings.py file does "solve" the problem, but now I'm wondering why. I obviously had that line in INSTALLED_APPS yesterday while I was still working, but I'm wondering why it suddenly stopped working today when I hadn't touched any code since then.

Community
  • 1
  • 1
3cheesewheel
  • 9,133
  • 9
  • 39
  • 59

1 Answers1

7

You need a Site object to be present in the sites table. For some reason, the Site object was not created.

Try this

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()

This should fix your problem.

You could also refer to this documentation about enabling the Sites framework:

To enable the sites framework, follow these steps:

  • Add 'django.contrib.sites' to your INSTALLED_APPS setting.

  • Define a SITE_ID setting:

    SITE_ID = 1

  • Run migrate.

django.contrib.sites registers a post_migrate signal handler which creates a default site named example.com with the domain example.com. This site will also be created after Django creates the test database. To set the correct name and domain for your project, you can use an initial data fixture.

karthikr
  • 97,368
  • 26
  • 197
  • 188