The following code will provide template access to your project settings. For security, it won't pass any setting with SECRET or PASSWORD in it.

Save as context_processors.py inside your project root (or anything you like, really)

# To use, add the following line to settings.py:
# TEMPLATE_CONTEXT_PROCESSORS = ('my_project.context_processors.settings',)
#
# Remember, you need to use RequestContext instead of Context in your views:
# from django.template import RequestContext
# return render_to_response('my_app/my_template.html', {'some_var': 'foo'}, 
#                           context_instance=RequestContext(request))

from django.views.debug import get_safe_settings

class SafeSettings:
    def __init__(self):
        self._settings = None
    
    def __getattr__(self, name):
        # Lazy load of settings.
        if self._settings is None:
            self._settings = get_safe_settings()
        # get_safe_settings only returns upper case settings, so let's not worry
        # about case sensitivity.
        name = name.upper()
        try:
            return self._settings[name]
        except KeyError:
            # This method should return the (computed) attribute value or raise 
            # an AttributeError exception.
            raise AttributeError
        
def settings(request):
    return {'settings': SafeSettings()}

-- code by SmileyChris

Last modified 18 years ago Last modified on Aug 13, 2006, 4:45:22 PM
Note: See TracWiki for help on using the wiki.
Back to Top