|
| 1 | +# Django设置[¶](#django-settings "Permalink to this headline") |
| 2 | + |
| 3 | +原文:[Django settings](https://docs.djangoproject.com/en/1.9/topics/settings/) |
| 4 | +--- |
| 5 | + |
| 6 | +一个Django设置文件包含你的Django安装的所有配置。本文介绍设置如何工作,以及哪些设置可用。 |
| 7 | + |
| 8 | + |
| 9 | +## 基础知识[¶](#the-basics "Permalink to this headline") |
| 10 | + |
| 11 | +设置文件仅仅是拥有模块级变量的Python模块。 |
| 12 | + |
| 13 | +这里有几个设置的例子: |
| 14 | +```python |
| 15 | +ALLOWED_HOSTS = ['www.example.com'] |
| 16 | +DEBUG = False |
| 17 | +DEFAULT_FROM_EMAIL = 'webmaster@example.com' |
| 18 | +``` |
| 19 | + |
| 20 | +>注意 |
| 21 | +
|
| 22 | +>如果设置[`DEBUG`](https://docs.djangoproject.com/en/1.9/ref/settings/#std:setting-DEBUG)为`False`, 那么还需要正确设置[`ALLOWED_HOSTS`](https://docs.djangoproject.com/en/1.9/ref/settings/#std:setting-ALLOWED_HOSTS)。 |
| 23 | +
|
| 24 | + |
| 25 | + |
| 26 | +由于一个设置文件是一个Python模块,因此应满足下面要求: |
| 27 | + |
| 28 | +* 不允许Python语法错误。 |
| 29 | + |
| 30 | +* 可以使用正常的Python语法动态的给设置项赋值。例如: |
| 31 | + |
| 32 | + `MY_SETTING = [str(i) for i in range(30)]`` |
| 33 | + |
| 34 | +* 可以从其他设置文件中导入值。 |
| 35 | + |
| 36 | + |
| 37 | +## 指定设置项[¶](#designating-the-settings "Permalink to this headline") |
| 38 | + |
| 39 | +`DJANGO_SETTINGS_MODULE`[¶](#envvar-DJANGO_SETTINGS_MODULE "Permalink to this definition") |
| 40 | + |
| 41 | +当你使用Django时,你必须告诉它你所使用的配置项。通过使用一个环境变量`DJANGO_SETTINGS_MODULE`来做到这一点。 |
| 42 | + |
| 43 | +`DJANGO_SETTINGS_MODULE`的值应该满足Python的路径语法,例如,`mysite.settings`。注意,settings模块应该在 |
| 44 | +Python的 [import搜索路径](http://www.diveintopython.net/getting_to_know_python/everything_is_an_object.html)上。 |
| 45 | + |
| 46 | + |
| 47 | +### django-admin功能[¶](#the-django-admin-utility "Permalink to this headline") |
| 48 | + |
| 49 | +当使用[_django-admin_](https://docs.djangoproject.com/en/1.9/ref/django-admin/)时, 你可以一次性设置环境变量,或者在每一次运行此功能时显式传入settings模块。 |
| 50 | + |
| 51 | +例子(Unix Bash shell): |
| 52 | +```python |
| 53 | +export DJANGO_SETTINGS_MODULE=mysite.settings |
| 54 | +django-admin runserver |
| 55 | +``` |
| 56 | + |
| 57 | +例子 (Windows shell): |
| 58 | +```python |
| 59 | +set DJANGO_SETTINGS_MODULE=mysite.settings |
| 60 | +django-admin runserver |
| 61 | +``` |
| 62 | + |
| 63 | +使用`--settings`命令行参数来手动指定settings: |
| 64 | + |
| 65 | +`django-admin runserver --settings=mysite.settings` |
| 66 | + |
| 67 | +### 在服务器上(mod_wsgi)[¶](#on-the-server-mod-wsgi "Permalink to this headline") |
| 68 | + |
| 69 | +在你的在线服务器环境中,你需要告诉你的WSGI应用使用哪一个设置文件。使用`os.environ`来做到这点: |
| 70 | +```python |
| 71 | +import os |
| 72 | + |
| 73 | +os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings' |
| 74 | +``` |
| 75 | + |
| 76 | +见[_Django mod_wsgi documentation_](https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/modwsgi/)以查看更多信息以及其他Django WSGI应用的常见元素。 |
| 77 | + |
| 78 | +## 默认设置项[¶](#default-settings "Permalink to this headline") |
| 79 | + |
| 80 | +如果不需要的话,Django的设置文件不定义任何设置项。每个设置都有一个合理的默认值。这些默认值位于模块`django/conf/global_settings.py`中。 |
| 81 | + |
| 82 | +这里是Django在编译设置项时使用的算法: |
| 83 | + |
| 84 | +* 从`global_settings.py`中加载设置项. |
| 85 | +* 从指定的设置文件中加载设置项,并在需要的时候覆盖全局设置。 |
| 86 | + |
| 87 | +注意,设置文件不应该从`global_settings`中导入,因为那是多余的。 |
| 88 | + |
| 89 | + |
| 90 | +### 看看你修改了哪些设置项[¶](#seeing-which-settings-you-ve-changed "Permalink to this headline") |
| 91 | + |
| 92 | +有一个简单的方法来看看哪些设置项是从默认设置项中继承过来的。命令`python manage.py diffsettings`显示当前设置文件和Django默认设置项之间的不同。 |
| 93 | + |
| 94 | +详细信息,见[`diffsettings`](https://docs.djangoproject.com/en/1.9/ref/django-admin/#django-admin-diffsettings)文档。 |
| 95 | + |
| 96 | + |
| 97 | +## 在Python代码中使用设置项[¶](#using-settings-in-python-code "Permalink to this headline") |
| 98 | + |
| 99 | +在你的Django应用中,通过导入对象`django.conf.settings`来使用设置项。例如: |
| 100 | +```python |
| 101 | +from django.conf import settings |
| 102 | + |
| 103 | +if settings.DEBUG: |
| 104 | + # Do something |
| 105 | +``` |
| 106 | + |
| 107 | +注意,`django.conf.settings`并不是一个模块 – 它是一个对象。所以不可能导入单独的设置项: |
| 108 | + |
| 109 | +`from django.conf.settings import DEBUG # This won't work.` |
| 110 | + |
| 111 | +另请注意,你的代码不应该从`global_settings`或者自己的设置文件中导入设置项。`django.conf.settings`抽象了默认配置项以及站点指定配置项的概念;它代表了一个单一的接口。它还解耦了使用从你的设置位置中导入的设置项的代码。 |
| 112 | + |
| 113 | + |
| 114 | +## 在运行时变更设置项[¶](#altering-settings-at-runtime "Permalink to this headline") |
| 115 | + |
| 116 | +你不应该在运行时更改应用中的设置项。例如,不要在一个视图中这样做: |
| 117 | +```python |
| 118 | +from django.conf import settings |
| 119 | + |
| 120 | +settings.DEBUG = True # Don't do this! |
| 121 | +``` |
| 122 | + |
| 123 | +设置项赋值的唯一一个地方就是设置文件。 |
| 124 | + |
| 125 | + |
| 126 | +## 安全[¶](#security "Permalink to this headline") |
| 127 | + |
| 128 | +因为设置文件包含敏感信息,如数据库的密码,你应该尽一切努力来限制对它的访问。例如,更改文件权限,以便只有你和你的Web服务器的用户可以读取它。这在一个共享托管环境中特别重要。 |
| 129 | + |
| 130 | + |
| 131 | +## 可用设置项[¶](#available-settings "Permalink to this headline") |
| 132 | + |
| 133 | +有关可用设置的完整列表,请参阅[设置参考](https://docs.djangoproject.com/en/1.9/ref/settings/). |
| 134 | + |
| 135 | + |
| 136 | +## 创建你自己的设置项[¶](#creating-your-own-settings "Permalink to this headline") |
| 137 | + |
| 138 | +没有什么可以阻止你为自己的Django应用创建自己的设置项。只是遵循以下约定: |
| 139 | + |
| 140 | +* 设置名称全部大写。 |
| 141 | +* 不要重新发明已经存在的设置。 |
| 142 | + |
| 143 | +对于那些是序列的设置项,Django自身使用列表类型,但这仅仅是一个惯例。 |
| 144 | + |
| 145 | + |
| 146 | +## 使用设置项而不设置DJANGO_SETTINGS_MODULE[¶](#using-settings-without-setting-django-settings-module "Permalink to this headline") |
| 147 | + |
| 148 | +在某些情况下,你可能想绕过`DJANGO_SETTINGS_MODULE`环境变量。例如,如果你使用自身的模板系统,你可能不希望必须建立一个环境变量来指向一个设置模块。 |
| 149 | + |
| 150 | +In these cases, you can configure Django’s settings manually. Do this by |
| 151 | +calling:在这些情况下,您可以手动配置Django的设置项。通过调用以下来执行此操作: |
| 152 | + |
| 153 | +`django.conf.settings.``configure`(_default_settings_, _**settings_)[¶](#django.conf.settings.configure "Permalink to this definition") |
| 154 | + |
| 155 | +例如: |
| 156 | +```python |
| 157 | +from django.conf import settings |
| 158 | + |
| 159 | +settings.configure(DEBUG=True) |
| 160 | +``` |
| 161 | + |
| 162 | +Pass `configure()` as many keyword arguments as you’d like, with each keyword |
| 163 | +argument representing a setting and its value. Each argument name should be all |
| 164 | +uppercase, with the same name as the settings described above. If a particular |
| 165 | +setting is not passed to `configure()` and is needed at some later point, |
| 166 | +Django will use the default setting value. |
| 167 | + |
| 168 | +Configuring Django in this fashion is mostly necessary – and, indeed, |
| 169 | +recommended – when you’re using a piece of the framework inside a larger |
| 170 | +application. |
| 171 | + |
| 172 | +Consequently, when configured via `settings.configure()`, Django will not |
| 173 | +make any modifications to the process environment variables (see the |
| 174 | +documentation of [`TIME_ZONE`](https://docs.djangoproject.com/en/1.9/ref/settings/#std:setting-TIME_ZONE) for why this would normally occur). It’s |
| 175 | +assumed that you’re already in full control of your environment in these |
| 176 | +cases. |
| 177 | + |
| 178 | +### Custom default settings[¶](#custom-default-settings "Permalink to this headline") |
| 179 | + |
| 180 | +If you’d like default values to come from somewhere other than |
| 181 | +`django.conf.global_settings`, you can pass in a module or class that |
| 182 | +provides the default settings as the `default_settings` argument (or as the |
| 183 | +first positional argument) in the call to `configure()`. |
| 184 | + |
| 185 | +In this example, default settings are taken from `myapp_defaults`, and the |
| 186 | +[`DEBUG`](https://docs.djangoproject.com/en/1.9/ref/settings/#std:setting-DEBUG) setting is set to `True`, regardless of its value in |
| 187 | +`myapp_defaults`: |
| 188 | +```python |
| 189 | +from django.conf import settings |
| 190 | +from myapp import myapp_defaults |
| 191 | + |
| 192 | +settings.configure(default_settings=myapp_defaults, DEBUG=True) |
| 193 | +``` |
| 194 | +The following example, which uses `myapp_defaults` as a positional argument, |
| 195 | +is equivalent: |
| 196 | + |
| 197 | +`settings.configure(myapp_defaults, DEBUG=True)` |
| 198 | + |
| 199 | +Normally, you will not need to override the defaults in this fashion. The |
| 200 | +Django defaults are sufficiently tame that you can safely use them. Be aware |
| 201 | +that if you do pass in a new default module, it entirely _replaces_ the Django |
| 202 | +defaults, so you must specify a value for every possible setting that might be |
| 203 | +used in that code you are importing. Check in |
| 204 | +`django.conf.settings.global_settings` for the full list. |
| 205 | + |
| 206 | + |
| 207 | +### Either configure() or DJANGO_SETTINGS_MODULE is required[¶](#either-configure-or-django-settings-module-is-required "Permalink to this headline") |
| 208 | + |
| 209 | +If you’re not setting the `DJANGO_SETTINGS_MODULE` environment variable, you |
| 210 | +_must_ call `configure()` at some point before using any code that reads |
| 211 | +settings. |
| 212 | + |
| 213 | +If you don’t set `DJANGO_SETTINGS_MODULE` and don’t call `configure()`, |
| 214 | +Django will raise an `ImportError` exception the first time a setting |
| 215 | +is accessed. |
| 216 | + |
| 217 | +If you set `DJANGO_SETTINGS_MODULE`, access settings values somehow, _then_ |
| 218 | +call `configure()`, Django will raise a `RuntimeError` indicating |
| 219 | +that settings have already been configured. There is a property just for this |
| 220 | +purpose: |
| 221 | + |
| 222 | +For example: |
| 223 | +```python |
| 224 | +from django.conf import settings |
| 225 | +if not settings.configured: |
| 226 | + settings.configure(myapp_defaults, DEBUG=True) |
| 227 | +``` |
| 228 | + |
| 229 | +Also, it’s an error to call `configure()` more than once, or to call |
| 230 | +`configure()` after any setting has been accessed. |
| 231 | + |
| 232 | +It boils down to this: Use exactly one of either `configure()` or |
| 233 | +`DJANGO_SETTINGS_MODULE`. Not both, and not neither. |
| 234 | + |
| 235 | +### Calling `django.setup()` is required for “standalone” Django usage[¶](#calling-django-setup-is-required-for-standalone-django-usage "Permalink to this headline") |
| 236 | + |
| 237 | +If you’re using components of Django “standalone” – for example, writing a |
| 238 | +Python script which loads some Django templates and renders them, or uses the |
| 239 | +ORM to fetch some data – there’s one more step you’ll need in addition to |
| 240 | +configuring settings. |
| 241 | + |
| 242 | +After you’ve either set <span class="target" id="index-0">[`DJANGO_SETTINGS_MODULE`](#envvar-DJANGO_SETTINGS_MODULE) or called |
| 243 | +`configure()`, you’ll need to call [`django.setup()`](https://docs.djangoproject.com/en/1.9/ref/applications/#django.setup "django.setup") to load your |
| 244 | +settings and populate Django’s application registry. For example: |
| 245 | +```python |
| 246 | +import django |
| 247 | +from django.conf import settings |
| 248 | +from myapp import myapp_defaults |
| 249 | + |
| 250 | +settings.configure(default_settings=myapp_defaults, DEBUG=True) |
| 251 | +django.setup() |
| 252 | + |
| 253 | +# Now this script or any imported module can use any part of Django it needs. |
| 254 | +from myapp import models |
| 255 | +``` |
| 256 | + |
| 257 | +Note that calling `django.setup()` is only necessary if your code is truly |
| 258 | +standalone. When invoked by your Web server, or through [_django-admin_](https://docs.djangoproject.com/en/1.9/ref/django-admin/), Django will handle this for you. |
| 259 | + |
| 260 | + |
| 261 | +See also |
| 262 | + |
| 263 | +[_The Settings Reference_](https://docs.djangoproject.com/en/1.9/ref/settings/) |
| 264 | +Contains the complete list of core and contrib app settings. |
0 commit comments