为什么从INSTALLED_APPS中删除“ django.contrib.sites”会解决我突然收到的“在/ admin /处出现DoesNotExist”错误? [英] Why does removing 'django.contrib.sites' from INSTALLED_APPS fix the 'DoesNotExist at /admin/' error that I suddenly got?
问题描述
昨天我设置了一个新的Django应用,当我停止工作时,管理员仍然可以。今天早上,我运行服务器,然后看到此错误页面:
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.
我没有触摸任何代码或对数据库进行任何更改。在寻找答案时,我看到了这篇文章,的确删除了<$ c我的 settings.py
中 INSTALLED_APPS
中的$ c>'django.contrib.sites'文件确实可以解决问题,但是现在我想知道为什么 。显然,昨天我还在工作的时候,我在 INSTALLED_APPS
中包含了该行,但是我想知道为什么自从那时以来我没有接触任何代码时,它突然停止了工作。
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.
推荐答案
您需要 Site
对象才能出现在网站
表。由于某些原因,未创建站点
对象。
You need a Site
object to be present in the sites
table. For some reason, the Site
object was not created.
尝试一下
登录到您的Django shell
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:
-
将django.contrib.sites添加到您的INSTALLED_APPS设置中。
Add 'django.contrib.sites' to your INSTALLED_APPS setting.
定义一个SITE_ID设置:
Define a SITE_ID setting:
SITE_ID = 1
SITE_ID = 1
运行迁移。
django.contrib.sites注册一个
post_migrate
信号处理程序,该信号处理程序创建一个名为example.com
的默认站点,其域名为example。 com
。在Django创建测试数据库后,还将创建此站点。要为您的项目设置正确的名称和域,您可以使用初始数据固定装置。
django.contrib.sites registers a
post_migrate
signal handler which creates a default site namedexample.com
with the domainexample.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.
这篇关于为什么从INSTALLED_APPS中删除“ django.contrib.sites”会解决我突然收到的“在/ admin /处出现DoesNotExist”错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!