Python Django项目中`from django.conf import settings`和`import settings`有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19976115/
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
What's the difference between `from django.conf import settings` and `import settings` in a Django project
提问by tzenderman
I'm reading up that most people do from django.conf import settingsbut I don't undertstand the difference to simply doing import settingsin a django project file. Can anyone explain the difference?
我正在阅读大多数人都这样做,from django.conf import settings但我不明白简单地import settings在 django 项目文件中做的区别。谁能解释一下区别?
采纳答案by juliocesar
import settingswill import the first python module named settings.pyfound 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).
import settings将导入在 中settings.py找到的第一个 python 模块sys.path,通常(在默认的 django 设置中)。它只允许访问您的站点定义的设置文件,该文件会覆盖 django 默认设置 ( django.conf.global_settings)。
So, if you try to access a valid django setting not specified in your settings file you will get an error.
因此,如果您尝试访问未在设置文件中指定的有效 django 设置,您将收到错误消息。
django.conf.settingsis 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.conf.settings不是一个文件,而是一个抽象概念、默认设置和站点特定设置的类。当您使用from django.conf import settings.
You can also find it in the django docs.
您也可以在django 文档中找到它。
Hope this helps.
希望这可以帮助。
回答by mansuetus
from django.conf import settingsis better option.
from django.conf import settings是更好的选择。
I use different settings files for the same django project (one for "live", one for "dev"), the first one will select the one being executed.
我为同一个 django 项目使用不同的设置文件(一个用于“live”,一个用于“dev”),第一个将选择正在执行的那个。

