django-configurations/README.rst

111 lines
3.5 KiB
ReStructuredText
Raw Normal View History

2012-07-21 13:56:04 +00:00
django-configurations
=====================
.. image:: https://secure.travis-ci.org/jezdez/django-configurations.png
:alt: Build Status
2013-03-27 17:23:55 +00:00
:target: https://travis-ci.org/jezdez/django-configurations
2012-07-21 13:56:04 +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.
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
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 9 we don't use the common tool
``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
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.
2013-06-26 10:11:15 +00:00
**Alternatively** you can use a special Django project template that is a copy
of the one included in Django 1.5.x. The following example assumes you're using
pip_ to install dependencies.::
# first install Django and django-configurations
pip install -r https://raw.github.com/jezdez/django-configurations/templates/1.5.x/requirements.txt
# then create your new Django project with the provided template
django-admin.py startproject mysite -v2 --template https://github.com/jezdez/django-configurations/archive/templates/1.5.x.zip
Now you have a default Django 1.5.x project in the ``mysite`` directory that uses
django-configurations.
.. _pip: http://pip-installer.org/