为什么从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?

查看:278
本文介绍了为什么从INSTALLED_APPS中删除“ django.contrib.sites”会解决我突然收到的“在/ admin /处出现DoesNotExist”错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我设置了一个新的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 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.

这篇关于为什么从INSTALLED_APPS中删除“ django.contrib.sites”会解决我突然收到的“在/ admin /处出现DoesNotExist”错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆