django-configurations/README.rst

108 lines
3.1 KiB
ReStructuredText
Raw Normal View History

2012-07-21 13:56:04 +00:00
django-configurations
=====================
.. image:: https://travis-ci.org/jazzband/django-configurations.svg?branch=master
2012-07-21 13:56:04 +00:00
:alt: Build Status
2015-12-17 09:22:38 +00:00
:target: https://travis-ci.org/jazzband/django-configurations
2012-07-21 13:56:04 +00:00
2015-12-17 09:22:38 +00:00
.. image:: https://jazzband.co/static/img/badge.svg
:alt: Jazzband
:target: https://jazzband.co/
2015-08-10 08:34:50 +00:00
django-configurations eases Django project configuration by relying
2012-07-21 13:56:04 +00:00
on the composability of Python classes. It extends the notion of
Django's module based settings loading with well established
object oriented programming patterns.
2014-02-12 14:27:38 +00:00
Check out the `documentation`__ for more complete examples.
.. __: http://django-configurations.readthedocs.org/en/latest/
2012-07-21 13:56:04 +00:00
Quickstart
----------
Install django-configurations:
.. code-block:: console
2012-07-21 13:56:04 +00:00
pip install django-configurations
Then subclass the included ``configurations.Configuration`` class in your
2012-07-21 19:43:18 +00:00
project's **settings.py** or any other module you're using to store the
settings constants, e.g.:
.. code-block:: python
# mysite/settings.py
2012-07-21 13:56:04 +00:00
from configurations import Configuration
2012-07-21 13:56:04 +00:00
class Dev(Configuration):
2012-07-21 13:56:04 +00:00
DEBUG = True
Set the ``DJANGO_CONFIGURATION`` environment variable to the name of the class
you just created, e.g. in bash:
.. code-block:: console
2012-07-21 13:56:04 +00:00
export DJANGO_CONFIGURATION=Dev
2012-07-21 13:56:04 +00:00
and the ``DJANGO_SETTINGS_MODULE`` environment variable to the module
import path as usual, e.g. in bash:
.. code-block:: console
2012-07-21 13:56:04 +00:00
export DJANGO_SETTINGS_MODULE=mysite.settings
*Alternatively* supply the ``--configuration`` option when using Django
management commands along the lines of Django's default ``--settings``
command line option, e.g.::
python manage.py runserver --settings=mysite.settings --configuration=Dev
2012-07-21 13:56:04 +00:00
To enable Django to use your configuration you now have to modify your
2012-07-21 19:43:18 +00:00
**manage.py** or **wsgi.py** script to use django-configurations's versions
of the appropriate starter functions, e.g. a typical **manage.py** using
django-configurations would look like this:
.. code-block:: python
:emphasize-lines: 10
2012-07-21 13:56:04 +00:00
#!/usr/bin/env python
2012-07-21 13:56:04 +00:00
import os
import sys
2012-07-21 16:35:58 +00:00
2012-07-21 13:56:04 +00:00
if __name__ == "__main__":
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
os.environ.setdefault('DJANGO_CONFIGURATION', 'Dev')
2012-07-21 16:35:58 +00:00
2012-07-21 13:56:04 +00:00
from configurations.management import execute_from_command_line
2012-07-21 16:35:58 +00:00
2012-07-21 13:56:04 +00:00
execute_from_command_line(sys.argv)
Notice in line 10 we don't use the common tool
2012-07-21 13:56:04 +00:00
``django.core.management.execute_from_command_line`` but instead
``configurations.management.execute_from_command_line``.
The same applies to your **wsgi.py** file, e.g.:
.. code-block:: python
:emphasize-lines: 6
2012-07-21 13:56:04 +00:00
import os
2012-07-21 16:35:58 +00:00
2012-07-21 13:56:04 +00:00
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
os.environ.setdefault('DJANGO_CONFIGURATION', 'Dev')
2012-07-21 16:35:58 +00:00
2012-07-21 13:56:04 +00:00
from configurations.wsgi import get_wsgi_application
application = get_wsgi_application()
Here we don't use the default ``django.core.wsgi.get_wsgi_application``
function but instead ``configurations.wsgi.get_wsgi_application``.
2013-06-26 10:11:15 +00:00
That's it! You can now use your project with ``manage.py`` and your favorite
2012-07-21 13:56:04 +00:00
WSGI enabled server.