Defining Constants in Django


Question

I want to have some constants in a Django Projects. For example, let's say a constant called MIN_TIME_TEST.

I would like to be able to access this constant in two places: from within my Python code, and from within any Templates.

What's the best way to go about doing this?

EDIT: To clarify, I know about Template Context Processors and about just putting things in settings.py or some other file and just importing.

My question is, how do I combine the two approaches without violating the "Don't Repeat Yourself" rule? Based on the answers so far, here's my approach:

I'd like to create a file called global_constants.py, which will have a list of constants (things like MIN_TIME_TEST = 5). I can import this file into any module to get the constants.

But now, I want to create the context processor which returns all of these constants. How can I go about doing this automatically, without having to list them again in a dictionary, like in John Mee's answer?

1
19
10/28/2010 2:24:47 PM

Accepted Answer

Both Luper and Vladimir are correct imho but you'll need both in order to complete your requirements.

  • Although, the constants don't need to be in the settings.py, you could put them anywhere and import them from that place into your view/model/module code. I sometimes put them into the __init__.py if I don't care to have them to be considered globally relevant.

  • a context processor like this will ensure that selected variables are globally in the template scope

    def settings(request):
        """
        Put selected settings variables into the default template context
        """
        from django.conf import settings
        return {
            'DOMAIN':     settings.DOMAIN,
            'GOOGLEMAPS_API_KEY': settings.GOOGLEMAPS_API_KEY,
        }
    

But this might be overkill if you're new to django; perhaps you're just asking how to put variables into the template scope...?

from django.conf import settings

...
# do stuff with settings.MIN_TIME_TEST as you wish

render_to_response("the_template.html", { 
    "MIN_TIME_TEST": settings.MIN_TIME_TEST 
}, context_instance=RequestContext(request)
17
10/28/2010 11:55:31 AM

To build on other people's answers, here's a simple way you'd implement this:

In your settings file:

GLOBAL_SETTINGS = {
    'MIN_TIME_TEST': 'blah',
    'RANDOM_GLOBAL_VAR': 'blah',
}

Then, building off of John Mee's context processor:

def settings(request):
    """
    Put selected settings variables into the default template context
    """
    from django.conf import settings
    return settings.GLOBAL_SETTINGS

This will resolve the DRY issue.

Or, if you only plan to use the global settings occasionally and want to call them from within the view:

def view_func(request):
    from django.conf import settings
    # function code here
    ctx = {} #context variables here
    ctx.update(settings.GLOBAL_SETTINGS)
    # whatever output you want here

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon