2008-08-24 06:25:40 +08:00
|
|
|
===============
|
|
|
|
Django settings
|
|
|
|
===============
|
|
|
|
|
|
|
|
A Django settings file contains all the configuration of your Django
|
|
|
|
installation. This document explains how settings work and which settings are
|
|
|
|
available.
|
|
|
|
|
|
|
|
The basics
|
|
|
|
==========
|
|
|
|
|
|
|
|
A settings file is just a Python module with module-level variables.
|
|
|
|
|
|
|
|
Here are a couple of example settings::
|
|
|
|
|
2014-12-18 05:51:42 +08:00
|
|
|
ALLOWED_HOSTS = ['www.example.com']
|
2008-08-24 06:25:40 +08:00
|
|
|
DEBUG = False
|
|
|
|
DEFAULT_FROM_EMAIL = 'webmaster@example.com'
|
|
|
|
|
2013-06-10 03:05:15 +08:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
If you set :setting:`DEBUG` to ``False``, you also need to properly set
|
|
|
|
the :setting:`ALLOWED_HOSTS` setting.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
Because a settings file is a Python module, the following apply:
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* It doesn't allow for Python syntax errors.
|
|
|
|
* It can assign settings dynamically using normal Python syntax.
|
|
|
|
For example::
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
MY_SETTING = [str(i) for i in range(30)]
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* It can import values from other settings files.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
.. _django-settings-module:
|
|
|
|
|
|
|
|
Designating the settings
|
|
|
|
========================
|
|
|
|
|
2013-01-01 21:12:42 +08:00
|
|
|
.. envvar:: DJANGO_SETTINGS_MODULE
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
When you use Django, you have to tell it which settings you're using. Do this
|
2020-04-30 18:12:05 +08:00
|
|
|
by using an environment variable, :envvar:`DJANGO_SETTINGS_MODULE`.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2020-04-30 18:12:05 +08:00
|
|
|
The value of :envvar:`DJANGO_SETTINGS_MODULE` should be in Python path syntax,
|
|
|
|
e.g. ``mysite.settings``. Note that the settings module should be on the
|
2008-08-24 06:25:40 +08:00
|
|
|
Python `import search path`_.
|
|
|
|
|
2018-10-23 21:03:00 +08:00
|
|
|
.. _import search path: https://www.diveinto.org/python3/your-first-python-program.html#importsearchpath
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
The ``django-admin`` utility
|
|
|
|
----------------------------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2014-07-26 19:21:52 +08:00
|
|
|
When using :doc:`django-admin </ref/django-admin>`, you can either set the
|
2008-08-24 06:25:40 +08:00
|
|
|
environment variable once, or explicitly pass in the settings module each time
|
|
|
|
you run the utility.
|
|
|
|
|
|
|
|
Example (Unix Bash shell)::
|
|
|
|
|
|
|
|
export DJANGO_SETTINGS_MODULE=mysite.settings
|
2014-07-26 19:21:52 +08:00
|
|
|
django-admin runserver
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Example (Windows shell)::
|
|
|
|
|
|
|
|
set DJANGO_SETTINGS_MODULE=mysite.settings
|
2014-07-26 19:21:52 +08:00
|
|
|
django-admin runserver
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Use the ``--settings`` command-line argument to specify the settings manually::
|
|
|
|
|
2014-07-26 19:21:52 +08:00
|
|
|
django-admin runserver --settings=mysite.settings
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2014-07-26 19:21:52 +08:00
|
|
|
.. _django-admin: ../django-admin/
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
On the server (``mod_wsgi``)
|
|
|
|
----------------------------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2010-08-28 10:40:57 +08:00
|
|
|
In your live server environment, you'll need to tell your WSGI
|
|
|
|
application what settings file to use. Do that with ``os.environ``::
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2010-08-28 10:40:57 +08:00
|
|
|
import os
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2010-08-28 10:40:57 +08:00
|
|
|
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
|
|
|
|
|
|
|
|
Read the :doc:`Django mod_wsgi documentation
|
2011-10-22 12:30:10 +08:00
|
|
|
</howto/deployment/wsgi/modwsgi>` for more information and other common
|
2010-08-28 10:40:57 +08:00
|
|
|
elements to a Django WSGI application.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Default settings
|
|
|
|
================
|
|
|
|
|
|
|
|
A Django settings file doesn't have to define any settings if it doesn't need
|
2008-10-06 19:18:30 +08:00
|
|
|
to. Each setting has a sensible default value. These defaults live in the
|
|
|
|
module :file:`django/conf/global_settings.py`.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Here's the algorithm Django uses in compiling settings:
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* Load settings from ``global_settings.py``.
|
|
|
|
* Load settings from the specified settings file, overriding the global
|
|
|
|
settings as necessary.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Note that a settings file should *not* import from ``global_settings``, because
|
|
|
|
that's redundant.
|
|
|
|
|
|
|
|
Seeing which settings you've changed
|
|
|
|
------------------------------------
|
|
|
|
|
2019-06-17 22:54:55 +08:00
|
|
|
The command ``python manage.py diffsettings`` displays differences between the
|
|
|
|
current settings file and Django's default settings.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
For more, see the :djadmin:`diffsettings` documentation.
|
|
|
|
|
|
|
|
Using settings in Python code
|
|
|
|
=============================
|
|
|
|
|
|
|
|
In your Django apps, use settings by importing the object
|
|
|
|
``django.conf.settings``. Example::
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
if settings.DEBUG:
|
|
|
|
# Do something
|
|
|
|
|
|
|
|
Note that ``django.conf.settings`` isn't a module -- it's an object. So
|
|
|
|
importing individual settings is not possible::
|
|
|
|
|
|
|
|
from django.conf.settings import DEBUG # This won't work.
|
|
|
|
|
|
|
|
Also note that your code should *not* import from either ``global_settings`` or
|
|
|
|
your own settings file. ``django.conf.settings`` abstracts the concepts of
|
|
|
|
default settings and site-specific settings; it presents a single interface.
|
|
|
|
It also decouples the code that uses settings from the location of your
|
|
|
|
settings.
|
|
|
|
|
|
|
|
Altering settings at runtime
|
|
|
|
============================
|
|
|
|
|
|
|
|
You shouldn't alter settings in your applications at runtime. For example,
|
|
|
|
don't do this in a view::
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
settings.DEBUG = True # Don't do this!
|
|
|
|
|
|
|
|
The only place you should assign to settings is in a settings file.
|
|
|
|
|
|
|
|
Security
|
|
|
|
========
|
|
|
|
|
|
|
|
Because a settings file contains sensitive information, such as the database
|
|
|
|
password, you should make every attempt to limit access to it. For example,
|
|
|
|
change its file permissions so that only you and your Web server's user can
|
|
|
|
read it. This is especially important in a shared-hosting environment.
|
|
|
|
|
|
|
|
Available settings
|
|
|
|
==================
|
|
|
|
|
2010-08-20 03:27:44 +08:00
|
|
|
For a full list of available settings, see the :doc:`settings reference </ref/settings>`.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Creating your own settings
|
|
|
|
==========================
|
|
|
|
|
|
|
|
There's nothing stopping you from creating your own settings, for your own
|
2019-06-17 22:54:55 +08:00
|
|
|
Django apps, but follow these guidelines:
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2016-05-03 01:27:41 +08:00
|
|
|
* Setting names must be all uppercase.
|
2011-10-14 08:12:01 +08:00
|
|
|
* Don't reinvent an already-existing setting.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2015-05-22 18:12:36 +08:00
|
|
|
For settings that are sequences, Django itself uses lists, but this is only
|
|
|
|
a convention.
|
2008-10-05 16:58:17 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. _settings-without-django-settings-module:
|
|
|
|
|
2020-04-30 18:12:05 +08:00
|
|
|
Using settings without setting :envvar:`DJANGO_SETTINGS_MODULE`
|
|
|
|
===============================================================
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2020-04-30 18:12:05 +08:00
|
|
|
In some cases, you might want to bypass the :envvar:`DJANGO_SETTINGS_MODULE`
|
2008-08-24 06:25:40 +08:00
|
|
|
environment variable. For example, if you're using the template system by
|
|
|
|
itself, you likely don't want to have to set up an environment variable
|
|
|
|
pointing to a settings module.
|
|
|
|
|
|
|
|
In these cases, you can configure Django's settings manually. Do this by
|
2008-10-06 19:18:30 +08:00
|
|
|
calling:
|
|
|
|
|
|
|
|
.. function:: django.conf.settings.configure(default_settings, **settings)
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Example::
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
2015-02-15 22:42:05 +08:00
|
|
|
settings.configure(DEBUG=True)
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Pass ``configure()`` as many keyword arguments as you'd like, with each keyword
|
|
|
|
argument representing a setting and its value. Each argument name should be all
|
|
|
|
uppercase, with the same name as the settings described above. If a particular
|
|
|
|
setting is not passed to ``configure()`` and is needed at some later point,
|
|
|
|
Django will use the default setting value.
|
|
|
|
|
|
|
|
Configuring Django in this fashion is mostly necessary -- and, indeed,
|
|
|
|
recommended -- when you're using a piece of the framework inside a larger
|
|
|
|
application.
|
|
|
|
|
|
|
|
Consequently, when configured via ``settings.configure()``, Django will not
|
2008-10-06 19:18:30 +08:00
|
|
|
make any modifications to the process environment variables (see the
|
|
|
|
documentation of :setting:`TIME_ZONE` for why this would normally occur). It's
|
|
|
|
assumed that you're already in full control of your environment in these
|
|
|
|
cases.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Custom default settings
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
If you'd like default values to come from somewhere other than
|
|
|
|
``django.conf.global_settings``, you can pass in a module or class that
|
|
|
|
provides the default settings as the ``default_settings`` argument (or as the
|
|
|
|
first positional argument) in the call to ``configure()``.
|
|
|
|
|
|
|
|
In this example, default settings are taken from ``myapp_defaults``, and the
|
2011-05-30 01:41:04 +08:00
|
|
|
:setting:`DEBUG` setting is set to ``True``, regardless of its value in
|
2008-08-24 06:25:40 +08:00
|
|
|
``myapp_defaults``::
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from myapp import myapp_defaults
|
|
|
|
|
|
|
|
settings.configure(default_settings=myapp_defaults, DEBUG=True)
|
|
|
|
|
|
|
|
The following example, which uses ``myapp_defaults`` as a positional argument,
|
|
|
|
is equivalent::
|
|
|
|
|
2012-05-04 02:52:56 +08:00
|
|
|
settings.configure(myapp_defaults, DEBUG=True)
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Normally, you will not need to override the defaults in this fashion. The
|
|
|
|
Django defaults are sufficiently tame that you can safely use them. Be aware
|
|
|
|
that if you do pass in a new default module, it entirely *replaces* the Django
|
|
|
|
defaults, so you must specify a value for every possible setting that might be
|
|
|
|
used in that code you are importing. Check in
|
|
|
|
``django.conf.settings.global_settings`` for the full list.
|
|
|
|
|
2020-04-30 18:12:05 +08:00
|
|
|
Either ``configure()`` or :envvar:`DJANGO_SETTINGS_MODULE` is required
|
|
|
|
----------------------------------------------------------------------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2020-04-30 18:12:05 +08:00
|
|
|
If you're not setting the :envvar:`DJANGO_SETTINGS_MODULE` environment
|
|
|
|
variable, you *must* call ``configure()`` at some point before using any code
|
|
|
|
that reads settings.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2020-04-30 18:12:05 +08:00
|
|
|
If you don't set :envvar:`DJANGO_SETTINGS_MODULE` and don't call
|
|
|
|
``configure()``, Django will raise an ``ImportError`` exception the first time
|
|
|
|
a setting is accessed.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2020-04-30 18:12:05 +08:00
|
|
|
If you set :envvar:`DJANGO_SETTINGS_MODULE`, access settings values somehow,
|
|
|
|
*then* call ``configure()``, Django will raise a ``RuntimeError`` indicating
|
2019-06-17 22:54:55 +08:00
|
|
|
that settings have already been configured. There is a property for this
|
2012-05-04 02:52:56 +08:00
|
|
|
purpose:
|
|
|
|
|
|
|
|
.. attribute: django.conf.settings.configured
|
|
|
|
|
|
|
|
For example::
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
if not settings.configured:
|
|
|
|
settings.configure(myapp_defaults, DEBUG=True)
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Also, it's an error to call ``configure()`` more than once, or to call
|
|
|
|
``configure()`` after any setting has been accessed.
|
|
|
|
|
|
|
|
It boils down to this: Use exactly one of either ``configure()`` or
|
2020-04-30 18:12:05 +08:00
|
|
|
:envvar:`DJANGO_SETTINGS_MODULE`. Not both, and not neither.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2015-07-22 01:12:42 +08:00
|
|
|
Calling ``django.setup()`` is required for "standalone" Django usage
|
|
|
|
--------------------------------------------------------------------
|
|
|
|
|
|
|
|
If you're using components of Django "standalone" -- for example, writing a
|
|
|
|
Python script which loads some Django templates and renders them, or uses the
|
|
|
|
ORM to fetch some data -- there's one more step you'll need in addition to
|
|
|
|
configuring settings.
|
|
|
|
|
|
|
|
After you've either set :envvar:`DJANGO_SETTINGS_MODULE` or called
|
|
|
|
``configure()``, you'll need to call :func:`django.setup()` to load your
|
|
|
|
settings and populate Django's application registry. For example::
|
|
|
|
|
2015-09-29 13:29:59 +08:00
|
|
|
import django
|
2015-07-22 01:12:42 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from myapp import myapp_defaults
|
|
|
|
|
|
|
|
settings.configure(default_settings=myapp_defaults, DEBUG=True)
|
|
|
|
django.setup()
|
|
|
|
|
2015-09-29 13:29:59 +08:00
|
|
|
# Now this script or any imported module can use any part of Django it needs.
|
|
|
|
from myapp import models
|
2015-07-22 01:12:42 +08:00
|
|
|
|
|
|
|
Note that calling ``django.setup()`` is only necessary if your code is truly
|
|
|
|
standalone. When invoked by your Web server, or through :doc:`django-admin
|
|
|
|
</ref/django-admin>`, Django will handle this for you.
|
2014-09-28 06:12:34 +08:00
|
|
|
|
2016-01-29 02:13:27 +08:00
|
|
|
.. admonition:: ``django.setup()`` may only be called once.
|
|
|
|
|
|
|
|
Therefore, avoid putting reusable application logic in standalone scripts
|
|
|
|
so that you have to import from the script elsewhere in your application.
|
|
|
|
If you can't avoid that, put the call to ``django.setup()`` inside an
|
|
|
|
``if`` block::
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import django
|
|
|
|
django.setup()
|
|
|
|
|
2014-09-28 06:12:34 +08:00
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
:doc:`The Settings Reference </ref/settings>`
|
|
|
|
Contains the complete list of core and contrib app settings.
|