APL
APL

Reputation: 11

Storing important singular values in Django

So I'm working on a website where there are a couple important values that get used in various places throughout the site. For example, certain important dates, like the start and end dates for registration.

One way I can do this is making a model that stores these values, but that sounds like overkill (since I'd only have one instance). Another way is to store these values in the settings.py file, but if I wanted to change them, it seems like I would need to restart the webserver for them to take effect. I was wondering what would be the best practice in Django to handle this kind of stuff.

Upvotes: 1

Views: 499

Answers (2)

ismail
ismail

Reputation: 3923

If you want a specific set of variables available to all of your template, what you are looking for is Context Processors.

http://docs.djangoproject.com/en/dev/ref/templates/api/#writing-your-own-context-processors

More links

http://www.b-list.org/weblog/2006/jun/14/django-tips-template-context-processors/ http://blog.madpython.com/2010/04/07/django-context-processors-best-practice/

The code for your context processors, can live anywhere in your project. You just have to add it to your settings.py under:

TEMPLATE_CONTEXT_PROCESSORS =

You could keep the define your constants in your settings.py or even under a constants.py and just

from constants import *

However as you mentioned, you would need to reload your server each time the settings are updated. I think you first need to figure out how often will you be changing these settings? Is it worth the extra effort to be able to reload the settings automatically?

If you wanted to automatically enable the settings, each time they are updated you could do the following:

  1. Store settings in the DB
  2. Upon save/change, write output to a file
  3. settings.py / constants.py reads files
  4. reload server

In addition, you have a look at the mezzanine project which allows you to update settings from the django admin interface and will reload as well.

See: http://mezzanine.jupo.org/docs/configuration.html

If the variables you need will be updated infrequently, i suggest just store them in settings.py and add a custom context processor.

If you are using source control such as GIT, updating will be quite easy, you can just update the file and push to your server. For really simple reloading of the server you could also create a post-recieve hook for git that will automatically reload the server when new code is pushed.

I would only suggest the other option if you are updating settings fairly regularly.

Upvotes: 0

VoY
VoY

Reputation: 5699

You can store them in settings.py. While there is nothing wrong with this (you can even organize your settings into multiple different files, if you have to many custom settings), you're right that you cannot change these at runtime.

We were solving the same problem where I work and came up with a simple app called django-constance (you can get it from github at https://github.com/comoga/django-constance). What this lets is store your settings in a settings.py, but once you need to turn them into settings configurable at runtime, you can switch to a Redis data store with django admin frontend. You can even use the value from settings as your default. I suggest you try this app out.

The changes to your code are pretty minimal, as pasted from docs you initialize your dynamic settings like this:

CONSTANCE_CONFIG = {
    'MY_SETTINGS_KEY': (42, 'the answer to everything'),
}

And then instead of importing settings from django conf, you do this:

from constance import config

if config.MY_SETTINGS_KEY == 42:
    answer_the_question()

Upvotes: 1

Related Questions