[英]How do I obtain django default settings values NOT defined in my settings.py file?
I must be missing something.我肯定错过了什么。
I'm trying to import DEFAULT_CONTENT_TYPE from the settings file.我正在尝试从设置文件中导入 DEFAULT_CONTENT_TYPE。 I don't define it specifically in my settings.py file, but I was assuming that the default would be available.
我没有在我的 settings.py 文件中专门定义它,但我假设默认值可用。
When I add this variable to my settings.py it works fine, but when it's not there and I try to import I get an ImportError when using the development server via the runserver command:当我将此变量添加到我的 settings.py 时,它工作正常,但是当它不存在并且我尝试导入时,我在通过 runserver 命令使用开发服务器时得到一个 ImportError:
from settings import DEFAULT_CONTENT_TYPE
ImportError: cannot import name DEFAULT_CONTENT_TYPE
Shouldn't I be able to do this without having to add it to my settings.py file?我不应该能够做到这一点而不必将其添加到我的settings.py 文件中吗?
Never user from settings import...
or import settings
.永远不要
from settings import...
或import settings
。 This will always only import your settings module, but not the whole Django settings.这将始终只导入您的设置模块,而不是整个 Django 设置。
Use from django.conf import settings
instead in all your Django projects.在所有 Django 项目中使用
from django.conf import settings
。 This is using the Django Settings class which is a wrapper around all Django defined settings and your project settings from your settings file.这是使用 Django 设置 class 包装所有 Django 定义的设置和您的设置文件中的项目设置。
So for your example you would access DEFAULT_CONTENT_TYPE
via settings.DEFAULT_CONTENT_TYPE
.因此,对于您的示例,您将通过
settings.DEFAULT_CONTENT_TYPE
访问DEFAULT_CONTENT_TYPE
。
settings
is relative to your project, so it'll try to import DEFAULT_CONTENT_TYPE
from your settings file. settings
与您的项目相关,因此它会尝试从您的设置文件中导入DEFAULT_CONTENT_TYPE
。 If you want to use Django's built-in DEFAULT_CONTENT_TYPE
setting, you'd have to include it from their libraries (sorry, don't have Django installed on this machine, otherwise I'd look up the file that it's defined in).如果你想使用 Django 的内置
DEFAULT_CONTENT_TYPE
设置,你必须从他们的库中包含它(抱歉,这台机器上没有安装 Django,否则我会查找它定义的文件)。
Something like this: from django.some.settings.file import DEFAULT_CONTENT_TYPE
像这样:
from django.some.settings.file import DEFAULT_CONTENT_TYPE
You should be able to find it by just grep
'ing through your Django lib dir.您应该可以通过
grep
通过您的 Django 库目录找到它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.