Python AttributeError: 'Settings' 对象没有属性 'ROOT_URLCONF'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12839213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:58:22  来源:igfitidea点击:

AttributeError: 'Settings' object has no attribute 'ROOT_URLCONF'

pythondjangosettingslocalhostattributeerror

提问by Jess

Following on from my last question Error: No module named psycopg2.extensions, I have updated my mac OS to Mountain Lion and installed Xcode. I have also installed psycopg2 using 'sudo port install py27-psycopg2'. I am now trying to run 'python manage.py runserver' but am receiving this error

继我的上一个问题Error: No module named psycopg2.extensions 之后,我已将我的 mac OS 更新为 Mountain Lion 并安装了 Xcode。我还使用“sudo port install py27-psycopg2”安装了 psycopg2。我现在正在尝试运行“python manage.py runserver”,但收到此错误

AttributeError: 'Settings' object has no attribute 'ROOT_URLCONF'

Any help on how to fix this and get my localhost running?

有关如何解决此问题并使我的本地主机运行的任何帮助?

回答by defuz

From django docs:

来自 Django文档

A Django settings file contains all the configuration of your Django installation. When you use Django, you have to tell it which settings you're using. Do this by using an environment variable, DJANGO_SETTINGS_MODULE.

The value of DJANGO_SETTINGS_MODULEshould be in Python path syntax, e.g. mysite.settings. Note that the settings module should be on the Python import search path.

Django 设置文件包含 Django 安装的所有配置。当您使用 Django 时,您必须告诉它您正在使用哪些设置。为此,请使用环境变量DJANGO_SETTINGS_MODULE.

的值DJANGO_SETTINGS_MODULE应采用 Python 路径语法,例如mysite.settings. 请注意,设置模块应位于 Python 导入搜索路径上。

And

ROOT_URLCONF

Default: Not defined

A string representing the full Python import path to your root URLconf. For example: "mydjangoapps.urls". Can be overridden on a per-request basis by setting the attribute urlconf on the incoming HttpRequest object. See How Django processes a requestfor details.

ROOT_URLCONF

默认值:未定义

表示根 URLconf 的完整 Python 导入路径的字符串。例如:“mydjangoapps.urls”。通过在传入的 HttpRequest 对象上设置 urlconf 属性,可以在每个请求的基础上覆盖。有关详细信息,请参阅Django 如何处理请求

回答by Alyson

If you're working on a Django project, the root of your project(ROOT_URLCONF=variable) isn't defined in your settings.pyfile, or at least it isn't defined properly.

如果您正在处理 Django 项目,则您的项目(ROOT_URLCONF=变量)的根目录未在您的settings.py文件中定义,或者至少未正确定义。

If you don't have a settings.pyfile, this block of code should fix the issue:

如果你没有settings.py文件,这段代码应该可以解决这个问题:

from django.conf import settings

settings.configure(
# ...
    ROOT_URLCONF=__name__,
# ...
    ),
)

What that does is specify the root of your project runserverknows where to find your project.

这样做是指定您的项目的根目录runserver知道在哪里可以找到您的项目。

If do have a settings.pyfile, then find that variable and add __name__to it.

如果确实有settings.py文件,则找到该变量并将其添加__name__到其中。