109

我正在阅读大多数人都在阅读的内容,from django.conf import settings但我不明白与简单地import settings在 django 项目文件中进行操作的区别。谁能解释其中的区别?

4

2 回答 2

133

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).

So, if you try to access a valid django setting not specified in your settings file you will get an error.

django.conf.settings is not a file but an object (see source) 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.

You can also find it in the django docs.

Hope this helps.

于 2013-11-14T11:41:56.367 回答
30

from django.conf import settings是更好的选择。

我为同一个 django 项目使用不同的设置文件(一个用于“live”,一个用于“dev”),第一个将选择正在执行的那个。

于 2013-11-14T11:12:29.973 回答