简体   繁体   English

如何获取未在我的 settings.py 文件中定义的 django 默认设置值?

[英]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.

相关问题 如何在我的 django 项目(扩展 AbstractBaseUser 模型)的 settings.py 文件中将我的身份验证后端设置为默认值 - how do i set my authentication backend as default in settings.py file of my django project(Extended AbstractBaseUser model) 如何将我的settings.py文件中的常量导入到Django控制台中? - How do I import constants from my settings.py file into my Django console? 我在Django中的settings.py文件太长了。 如何将其分成两部分? - My settings.py file in Django is getting too long. How do I split it into two? 如何在我的settings.py中的DEBUG变量在Django的模板中可用? - How do I make the DEBUG variable in my settings.py available in my templates in Django? Django,更改settings.py的默认位置或使用其他文件 - Django, changing the default location of settings.py or use another file 如何根据环境从我的 settings.py DATABASES 数组中激活数据库设置? - How do I activate database settings from my settings.py DATABASES array based on environment? 我的django应用程序的settings.py文件中的此代码有什么问题? - what is wrong with this code in settings.py file of my django application? 无法在 django 中保存对我的 settings.py 文件的更改 - Can't save changes to my settings.py file in django 如何使用 postgresql 为生产配置我的 django settings.py - How to configure my django settings.py for production using postgresql settings.py文件中的Django AttributeError - Django AttributeError in settings.py file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM