2010-12-01 18:29:10 +00:00
|
|
|
Dynamic Django settings
|
|
|
|
|
=======================
|
2010-08-25 08:30:58 +00:00
|
|
|
|
|
|
|
|
Features
|
2010-12-01 18:29:10 +00:00
|
|
|
--------
|
2010-08-25 08:30:58 +00:00
|
|
|
|
2010-12-01 18:45:05 +00:00
|
|
|
* Easily migrate your static settings to dynamic settings.
|
2010-08-25 08:30:58 +00:00
|
|
|
* Admin interface to edit the dynamic settings.
|
|
|
|
|
|
|
|
|
|
Installation
|
2010-12-01 18:29:10 +00:00
|
|
|
------------
|
2010-08-25 08:30:58 +00:00
|
|
|
|
2010-12-01 18:29:10 +00:00
|
|
|
Install from PyPI::
|
|
|
|
|
|
|
|
|
|
pip install django-constance
|
|
|
|
|
|
|
|
|
|
Or install the `in-development version`_ using ``pip``::
|
2010-08-25 08:30:58 +00:00
|
|
|
|
2011-06-28 10:57:21 +00:00
|
|
|
pip install -e git+git://github.com/comoga/django-constance#egg=django-constance
|
2010-08-25 08:30:58 +00:00
|
|
|
|
2011-06-28 10:57:21 +00:00
|
|
|
.. _`in-development version`: https://github.com/comoga/django-constance/tarball/master#egg=django-constance-dev
|
2010-12-01 18:29:10 +00:00
|
|
|
|
2010-08-25 09:24:40 +00:00
|
|
|
Configuration
|
2010-12-01 18:29:10 +00:00
|
|
|
-------------
|
2010-08-25 08:30:58 +00:00
|
|
|
|
2010-12-01 18:45:05 +00:00
|
|
|
Modify your ``settings.py``. Add ``'constance'`` to your ``INSTALLED_APPS``,
|
2010-12-01 18:29:10 +00:00
|
|
|
and move each key you want to turn dynamic into the ``CONSTANCE_CONFIG``
|
|
|
|
|
section, like this::
|
2010-08-25 08:30:58 +00:00
|
|
|
|
2010-08-25 09:24:40 +00:00
|
|
|
INSTALLED_APPS = (
|
|
|
|
|
...
|
|
|
|
|
'constance',
|
|
|
|
|
)
|
2010-08-25 08:30:58 +00:00
|
|
|
|
2010-08-25 09:24:40 +00:00
|
|
|
CONSTANCE_CONFIG = {
|
|
|
|
|
'MY_SETTINGS_KEY': (42, 'the answer to everything'),
|
|
|
|
|
}
|
2010-08-25 08:30:58 +00:00
|
|
|
|
2010-12-01 18:45:05 +00:00
|
|
|
Here, ``42`` is the default value for the key ``MY_SETTINGS_KEY`` if it is
|
|
|
|
|
not found in the backend. The other member of the tuple is a help text the
|
|
|
|
|
admin will show.
|
2010-08-25 12:11:26 +00:00
|
|
|
|
2010-12-01 18:29:10 +00:00
|
|
|
See the `Backends`_ section how to setup the backend.
|
|
|
|
|
|
|
|
|
|
Backends
|
|
|
|
|
~~~~~~~~
|
|
|
|
|
|
2011-02-05 18:30:48 +00:00
|
|
|
Constance ships with a bunch of backends that are used to store the
|
|
|
|
|
configuration values. By default it uses the Redis backend. To override
|
|
|
|
|
the default please set the ``CONSTANCE_BACKEND`` setting to the appropriate
|
|
|
|
|
dotted path.
|
2010-12-01 18:29:10 +00:00
|
|
|
|
2011-02-05 18:30:48 +00:00
|
|
|
Redis (default)
|
|
|
|
|
+++++++++++++++
|
2010-12-01 18:29:10 +00:00
|
|
|
|
2011-02-05 18:30:48 +00:00
|
|
|
::
|
2010-12-01 18:29:10 +00:00
|
|
|
|
2011-02-05 18:30:48 +00:00
|
|
|
CONSTANCE_BACKEND = constance.backends.redisd.RedisBackend
|
2010-12-01 18:29:10 +00:00
|
|
|
|
2011-02-05 18:30:48 +00:00
|
|
|
The is the default backend and has a couple of options:
|
2010-12-01 18:29:10 +00:00
|
|
|
|
2011-02-05 18:30:48 +00:00
|
|
|
* ``CONSTANCE_REDIS_CONNECTION``
|
2010-12-01 18:29:10 +00:00
|
|
|
|
2011-02-05 18:30:48 +00:00
|
|
|
A dictionary of parameters to pass to the to Redis client, e.g.::
|
2010-12-01 18:29:10 +00:00
|
|
|
|
2011-02-05 18:30:48 +00:00
|
|
|
CONSTANCE_REDIS_CONNECTION = {
|
|
|
|
|
'host': 'localhost',
|
|
|
|
|
'port': 6379,
|
|
|
|
|
'db': 0,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
* ``CONSTANCE_REDIS_CONNECTION_CLASS``
|
|
|
|
|
|
|
|
|
|
An (optional) dotted import path to a connection to use, e.g.::
|
|
|
|
|
|
|
|
|
|
CONSTANCE_REDIS_CONNECTION_CLASS = 'myproject.myapp.mockup.Connection'
|
|
|
|
|
|
|
|
|
|
* ``CONSTANCE_REDIS_PREFIX``
|
|
|
|
|
|
|
|
|
|
The (optional) prefix to be used for the key when storing in the Redis
|
|
|
|
|
database. Defaults to ``'constance:'``. E.g.::
|
2010-12-01 18:29:10 +00:00
|
|
|
|
2011-02-05 18:30:48 +00:00
|
|
|
CONSTANCE_REDIS_PREFIX = 'constance:myproject:'
|
2010-12-01 18:29:10 +00:00
|
|
|
|
2011-02-05 18:30:48 +00:00
|
|
|
Database
|
|
|
|
|
++++++++
|
2010-12-01 18:29:10 +00:00
|
|
|
|
2011-02-05 18:30:48 +00:00
|
|
|
::
|
2010-12-01 18:29:10 +00:00
|
|
|
|
2011-02-05 18:30:48 +00:00
|
|
|
CONSTANCE_BACKEND = constance.backends.database.DatabaseBackend
|
|
|
|
|
|
|
|
|
|
If you want to use this backend you also need to add the databse backend
|
|
|
|
|
to your ``INSTALLED_APPS`` setting to make sure the data model is correctly
|
|
|
|
|
created::
|
|
|
|
|
|
|
|
|
|
INSTALLED_APPS = (
|
|
|
|
|
# other apps
|
|
|
|
|
'constance.backends.database',
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
It also uses `django-picklefield`_ to store the values in the database, so
|
|
|
|
|
you need to install this library, too. E.g.::
|
2010-12-01 18:29:10 +00:00
|
|
|
|
|
|
|
|
pip install django-picklefield
|
|
|
|
|
|
2011-02-05 18:30:48 +00:00
|
|
|
The database backend has the ability to automatically cache the config
|
|
|
|
|
values and clear them when saving. You need to set the following setting
|
|
|
|
|
to enable this feature::
|
2010-12-11 15:43:31 +00:00
|
|
|
|
|
|
|
|
CONSTANCE_DATABASE_CACHE_BACKEND = 'memcached://127.0.0.1:11211/'
|
|
|
|
|
|
2011-02-05 18:30:48 +00:00
|
|
|
.. note:: This won't work with a cache backend that doesn't support
|
|
|
|
|
cross-process caching, because correct cache invalidation
|
|
|
|
|
can't be guaranteed.
|
2010-12-01 18:29:10 +00:00
|
|
|
|
|
|
|
|
.. _django-picklefield: http://pypi.python.org/pypi/django-picklefield/
|
|
|
|
|
|
2010-08-25 08:30:58 +00:00
|
|
|
Usage
|
2010-12-01 18:29:10 +00:00
|
|
|
-----
|
2010-08-25 08:30:58 +00:00
|
|
|
|
2010-12-01 18:29:10 +00:00
|
|
|
Constance can be used from your Python code and from your Django templates.
|
|
|
|
|
|
|
|
|
|
* Python
|
|
|
|
|
|
|
|
|
|
Accessing the config variables is as easy as importing the config
|
|
|
|
|
object and accessing the variables with attribute lookups::
|
2010-08-25 08:30:58 +00:00
|
|
|
|
|
|
|
|
from constance import config
|
|
|
|
|
|
2011-01-24 14:00:41 +00:00
|
|
|
# ...
|
2010-08-25 08:30:58 +00:00
|
|
|
|
2010-08-25 09:24:40 +00:00
|
|
|
if config.MY_SETTINGS_KEY == 42:
|
|
|
|
|
answer_the_question()
|
2010-08-25 08:30:58 +00:00
|
|
|
|
2010-12-01 18:29:10 +00:00
|
|
|
* Django templates
|
|
|
|
|
|
|
|
|
|
To access the config object from your template, you can either
|
|
|
|
|
pass the object to the template context::
|
|
|
|
|
|
|
|
|
|
from django.shortcuts import render_to_response
|
|
|
|
|
from constance import config
|
|
|
|
|
|
|
|
|
|
def myview(request):
|
|
|
|
|
return render_to_response('my_template.html', {'config': config})
|
|
|
|
|
|
|
|
|
|
Or you can use the included config context processor.::
|
|
|
|
|
|
|
|
|
|
TEMPLATE_CONTEXT_PROCESSORS = (
|
|
|
|
|
# ...
|
|
|
|
|
'constance.context_processors.config',
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
This will add the config instance to the context of any template
|
|
|
|
|
rendered with a ``RequestContext``.
|
|
|
|
|
|
2010-12-01 18:45:05 +00:00
|
|
|
Then, in your template you can refer to the config values just as
|
2010-12-01 18:29:10 +00:00
|
|
|
any other variable, e.g.::
|
|
|
|
|
|
|
|
|
|
<h1>Welcome on {% config.SITE_NAME %}</h1>
|
|
|
|
|
{% if config.BETA_LAUNCHED %}
|
|
|
|
|
Woohoo! Head over <a href="/sekrit/">here</a> to use the beta.
|
|
|
|
|
{% else %}
|
|
|
|
|
Sadly we haven't launched yet, click <a href="/newsletter/">here</a>
|
|
|
|
|
to signup for our newletter.
|
|
|
|
|
{% endif %}
|
|
|
|
|
|
|
|
|
|
Editing
|
|
|
|
|
~~~~~~~
|
2010-08-25 08:30:58 +00:00
|
|
|
|
2010-12-01 18:45:05 +00:00
|
|
|
Fire up your ``admin`` and you should see a new app called ``Constance``
|
2010-08-25 08:30:58 +00:00
|
|
|
with ``MY_SETTINGS_KEY`` in the ``Config`` pseudo model.
|
|
|
|
|
|
2010-09-03 14:39:16 +00:00
|
|
|
Screenshots
|
2011-02-05 18:30:48 +00:00
|
|
|
-----------
|
2010-09-03 14:39:16 +00:00
|
|
|
|
2011-06-28 10:57:21 +00:00
|
|
|
.. figure:: https://github.com/comoga/django-constance/raw/master/docs/screenshot2.png
|
2010-09-03 14:39:16 +00:00
|
|
|
|
|
|
|
|
The standard edit screen.
|
|
|
|
|
|
2011-06-28 10:57:21 +00:00
|
|
|
.. figure:: https://github.com/comoga/django-constance/raw/master/docs/screenshot1.png
|
2010-09-03 14:39:16 +00:00
|
|
|
|
|
|
|
|
The virtual application ``Constance`` among your regular applications.
|
|
|
|
|
|
|
|
|
|
|