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:
ALLOWED_HOSTS = ['www.example.com']
DEBUG = False
DEFAULT_FROM_EMAIL = 'webmaster@example.com'
Nota
If you set DEBUG to False, you also need to properly set
the ALLOWED_HOSTS setting.
Because a settings file is a Python module, the following apply:
It doesn’t allow for Python syntax errors.
It can assign settings dynamically using normal Python syntax. For example:
MY_SETTING = [str(i) for i in range(30)]
It can import values from other settings files.
Designating the settings¶
-
DJANGO_SETTINGS_MODULE¶
When you use Django, you have to tell it which settings you’re using. Do this
by using an environment variable, DJANGO_SETTINGS_MODULE.
The value of DJANGO_SETTINGS_MODULE should be in Python path syntax, e.g.
mysite.settings. Note that the settings module should be on the
Python import search path.
The django-admin utility¶
When using django-admin, you can either set the 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
django-admin runserver
Example (Windows shell):
set DJANGO_SETTINGS_MODULE=mysite.settings
django-admin runserver
Use the --settings command-line argument to specify the settings manually:
django-admin runserver --settings=mysite.settings
On the server (mod_wsgi)¶
In your live server environment, you’ll need to tell your WSGI
application what settings file to use. Do that with os.environ:
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
Read the Django mod_wsgi documentation for more information and other common elements to a Django WSGI application.
Default settings¶
A Django settings file doesn’t have to define any settings if it doesn’t need
to. Each setting has a sensible default value. These defaults live in the
module django/conf/global_settings.py.
Here’s the algorithm Django uses in compiling settings:
- Load settings from
global_settings.py. - Load settings from the specified settings file, overriding the global settings as necessary.
Note that a settings file should not import from global_settings, because
that’s redundant.
Seeing which settings you’ve changed¶
There’s an easy way to view which of your settings deviate from the default
settings. The command python manage.py diffsettings displays differences
between the current settings file and Django’s default settings.
For more, see the