Django项目中的“ from django.conf导入设置”和“导入设置”之间有什么区别 [英] What's the difference between `from django.conf import settings` and `import settings` in a Django project
问题描述
我了解到大多数人都从django.conf导入设置中执行
,但我不理解仅执行导入设置的区别
在django项目文件中。谁能解释其中的区别?
I'm reading up that most people do from django.conf import settings
but I don't undertstand the difference to simply doing import settings
in a django project file. Can anyone explain the difference?
推荐答案
导入设置
将导入第一个通常在 sys.path
中找到名为 settings.py
的python模块(默认为django设置)。它仅允许访问您站点定义的设置文件,该文件将覆盖django默认设置( django.conf.global_settings
)。
import settings
will import the first python module named settings.py
found in sys.path
, usually (in default django setups). It allows access only to your site defined settings file, which overwrites django default settings (django.conf.global_settings
).
因此,如果您尝试访问未在设置文件中指定的有效django设置,则会收到错误消息。
So, if you try to access a valid django setting not specified in your settings file you will get an error.
django.conf。 settings
不是文件,而是对概念,默认设置和站点特定设置进行抽象的类。当您使用django.conf导入设置中的
时,Django还会进行其他检查。
django.conf.settings
is not a file but a class making an abstraction of the concepts, default settings and your site-specific settings. Django also does other checks when you use from django.conf import settings
.
您也可以在 django文档。
希望这会有所帮助。
这篇关于Django项目中的“ from django.conf导入设置”和“导入设置”之间有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!