Debian Codegraph @

docs/topics/settings.txt

raw
1 ===============
2 Django settings
3 ===============
4
5 A Django settings file contains all the configuration of your Django
6 installation. This document explains how settings work and which settings are
7 available.
8
9 The basics
10 ==========
11
12 A settings file is just a Python module with module-level variables.
13
14 Here are a couple of example settings::
15
16 ALLOWED_HOSTS = ["www.example.com"]
17 DEBUG = False
18 DEFAULT_FROM_EMAIL = "[email protected]"
19
20 .. note::
21
22 If you set :setting:`DEBUG` to ``False``, you also need to properly set
23 the :setting:`ALLOWED_HOSTS` setting.
24
25 Because a settings file is a Python module, the following apply:
26
27 * It doesn't allow for Python syntax errors.
28 * It can assign settings dynamically using normal Python syntax.
29 For example::
30
31 MY_SETTING = [str(i) for i in range(30)]
32
33 * It can import values from other settings files.
34
35 .. _django-settings-module:
36
37 Designating the settings
38 ========================
39
40 .. envvar:: DJANGO_SETTINGS_MODULE
41
42 When you use Django, you have to tell it which settings you're using. Do this
43 by using an environment variable, :envvar:`DJANGO_SETTINGS_MODULE`.
44
45 The value of :envvar:`DJANGO_SETTINGS_MODULE` should be in Python path syntax,
46 e.g. ``mysite.settings``. Note that the settings module should be on the
47 Python :py:data:`sys.path`.
48
49
50 The ``django-admin`` utility
51 ----------------------------
52
53 When using :doc:`django-admin </ref/django-admin>`, you can either set the
54 environment variable once, or explicitly pass in the settings module each time
55 you run the utility.
56
57 Example (Unix Bash shell):
58
59 .. code-block:: shell
60
61 export DJANGO_SETTINGS_MODULE=mysite.settings
62 django-admin runserver
63
64 Example (Windows shell):
65
66 .. code-block:: doscon
67
68 set DJANGO_SETTINGS_MODULE=mysite.settings
69 django-admin runserver
70
71 Use the ``--settings`` command-line argument to specify the settings manually:
72
73 .. code-block:: shell
74
75 django-admin runserver --settings=mysite.settings
76
77 .. _django-admin: ../django-admin/
78
79 On the server (``mod_wsgi``)
80 ----------------------------
81
82 In your live server environment, you'll need to tell your WSGI
83 application what settings file to use. Do that with ``os.environ``::
84
85 import os
86
87 os.environ["DJANGO_SETTINGS_MODULE"] = "mysite.settings"
88
89 Read the :doc:`Django mod_wsgi documentation
90 </howto/deployment/wsgi/modwsgi>` for more information and other common
91 elements to a Django WSGI application.
92
93 Default settings
94 ================
95
96 A Django settings file doesn't have to define any settings if it doesn't need
97 to. Each setting has a sensible default value. These defaults live in the
98 module :source:`django/conf/global_settings.py`.
99
100 Here's the algorithm Django uses in compiling settings:
101
102 * Load settings from ``global_settings.py``.
103 * Load settings from the specified settings file, overriding the global
104 settings as necessary.
105
106 Note that a settings file should *not* import from ``global_settings``, because
107 that's redundant.
108
109 Seeing which settings you've changed
110 ------------------------------------
111
112 The command ``python manage.py diffsettings`` displays differences between the
113 current settings file and Django's default settings.
114
115 For more, see the :djadmin:`diffsettings` documentation.
116
117 Using settings in Python code
118 =============================
119
120 In your Django apps, use settings by importing the object
121 ``django.conf.settings``. Example::
122
123 from django.conf import settings
124
125 if settings.DEBUG:
126 # Do something
127 ...
128
129 Note that ``django.conf.settings`` isn't a module -- it's an object. So
130 importing individual settings is not possible::
131
132 from django.conf.settings import DEBUG # This won't work.
133
134 Also note that your code should *not* import from either ``global_settings`` or
135 your own settings file. ``django.conf.settings`` abstracts the concepts of
136 default settings and site-specific settings; it presents a single interface.
137 It also decouples the code that uses settings from the location of your
138 settings.
139
140 Altering settings at runtime
141 ============================
142
143 You shouldn't alter settings in your applications at runtime. For example,
144 don't do this in a view::
145
146 from django.conf import settings
147
148 settings.DEBUG = True # Don't do this!
149
150 The only place you should assign to settings is in a settings file.
151
152 Security
153 ========
154
155 Because a settings file contains sensitive information, such as the database
156 password, you should make every attempt to limit access to it. For example,
157 change its file permissions so that only you and your web server's user can
158 read it. This is especially important in a shared-hosting environment.
159
160 Available settings
161 ==================
162
163 For a full list of available settings, see the :doc:`settings reference </ref/settings>`.
164
165 Creating your own settings
166 ==========================
167
168 There's nothing stopping you from creating your own settings, for your own
169 Django apps, but follow these guidelines:
170
171 * Setting names must be all uppercase.
172 * Don't reinvent an already-existing setting.
173
174 For settings that are sequences, Django itself uses lists, but this is only
175 a convention.
176
177 .. _settings-without-django-settings-module:
178
179 Using settings without setting :envvar:`DJANGO_SETTINGS_MODULE`
180 ===============================================================
181
182 In some cases, you might want to bypass the :envvar:`DJANGO_SETTINGS_MODULE`
183 environment variable. For example, if you're using the template system by
184 itself, you likely don't want to have to set up an environment variable
185 pointing to a settings module.
186
187 In these cases, you can configure Django's settings manually. Do this by
188 calling:
189
190 .. function:: django.conf.settings.configure(default_settings, **settings)
191
192 Example::
193
194 from django.conf import settings
195
196 settings.configure(DEBUG=True)
197
198 Pass ``configure()`` as many keyword arguments as you'd like, with each keyword
199 argument representing a setting and its value. Each argument name should be all
200 uppercase, with the same name as the settings described above. If a particular
201 setting is not passed to ``configure()`` and is needed at some later point,
202 Django will use the default setting value.
203
204 Configuring Django in this fashion is mostly necessary -- and, indeed,
205 recommended -- when you're using a piece of the framework inside a larger
206 application.
207
208 Consequently, when configured via ``settings.configure()``, Django will not
209 make any modifications to the process environment variables (see the
210 documentation of :setting:`TIME_ZONE` for why this would normally occur). It's
211 assumed that you're already in full control of your environment in these
212 cases.
213
214 Custom default settings
215 -----------------------
216
217 If you'd like default values to come from somewhere other than
218 ``django.conf.global_settings``, you can pass in a module or class that
219 provides the default settings as the ``default_settings`` argument (or as the
220 first positional argument) in the call to ``configure()``.
221
222 In this example, default settings are taken from ``myapp_defaults``, and the
223 :setting:`DEBUG` setting is set to ``True``, regardless of its value in
224 ``myapp_defaults``::
225
226 from django.conf import settings
227 from myapp import myapp_defaults
228
229 settings.configure(default_settings=myapp_defaults, DEBUG=True)
230
231 The following example, which uses ``myapp_defaults`` as a positional argument,
232 is equivalent::
233
234 settings.configure(myapp_defaults, DEBUG=True)
235
236 Normally, you will not need to override the defaults in this fashion. The
237 Django defaults are sufficiently tame that you can safely use them. Be aware
238 that if you do pass in a new default module, it entirely *replaces* the Django
239 defaults, so you must specify a value for every possible setting that might be
240 used in the code you are importing. Check in
241 ``django.conf.settings.global_settings`` for the full list.
242
243 Either ``configure()`` or :envvar:`DJANGO_SETTINGS_MODULE` is required
244 ----------------------------------------------------------------------
245
246 If you're not setting the :envvar:`DJANGO_SETTINGS_MODULE` environment
247 variable, you *must* call ``configure()`` at some point before using any code
248 that reads settings.
249
250 If you don't set :envvar:`DJANGO_SETTINGS_MODULE` and don't call
251 ``configure()``, Django will raise an ``ImportError`` exception the first time
252 a setting is accessed.
253
254 If you set :envvar:`DJANGO_SETTINGS_MODULE`, access settings values somehow,
255 *then* call ``configure()``, Django will raise a ``RuntimeError`` indicating
256 that settings have already been configured. There is a property for this
257 purpose:
258
259 .. attribute:: django.conf.settings.configured
260
261 For example::
262
263 from django.conf import settings
264
265 if not settings.configured:
266 settings.configure(myapp_defaults, DEBUG=True)
267
268 Also, it's an error to call ``configure()`` more than once, or to call
269 ``configure()`` after any setting has been accessed.
270
271 It boils down to this: Use exactly one of either ``configure()`` or
272 :envvar:`DJANGO_SETTINGS_MODULE`. Not both, and not neither.
273
274 Calling ``django.setup()`` is required for "standalone" Django usage
275 --------------------------------------------------------------------
276
277 If you're using components of Django "standalone" -- for example, writing a
278 Python script which loads some Django templates and renders them, or uses the
279 ORM to fetch some data -- there's one more step you'll need in addition to
280 configuring settings.
281
282 After you've either set :envvar:`DJANGO_SETTINGS_MODULE` or called
283 ``configure()``, you'll need to call :func:`django.setup()` to load your
284 settings and populate Django's application registry. For example::
285
286 import django
287 from django.conf import settings
288 from myapp import myapp_defaults
289
290 settings.configure(default_settings=myapp_defaults, DEBUG=True)
291 django.setup()
292
293 # Now this script or any imported module can use any part of Django it needs.
294 from myapp import models
295
296 Note that calling ``django.setup()`` is only necessary if your code is truly
297 standalone. When invoked by your web server, or through :doc:`django-admin
298 </ref/django-admin>`, Django will handle this for you.
299
300 .. admonition:: ``django.setup()`` may only be called once.
301
302 Therefore, avoid putting reusable application logic in standalone scripts
303 so that you have to import from the script elsewhere in your application.
304 If you can't avoid that, put the call to ``django.setup()`` inside an
305 ``if`` block::
306
307 if __name__ == "__main__":
308 import django
309
310 django.setup()
311
312 .. seealso::
313
314 :doc:`The Settings Reference </ref/settings>`
315 Contains the complete list of core and contrib app settings.