Changes between Initial Version and Version 1 of SafeSettingsContextProcessor


Ignore:
Timestamp:
Aug 13, 2006, 4:43:30 PM (18 years ago)
Author:
Chris Beaven
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • SafeSettingsContextProcessor

    v1 v1  
     1The following code will provide template access to your project settings. For security, it won't pass any setting with SECRET or PASSWORD in it.
     2
     3Save as `context_processors.py` inside your project root (or anything you like, really)
     4{{{
     5#!python
     6# To use, add the following line to settings.py:
     7# TEMPLATE_CONTEXT_PROCESSORS = ('my_project.context_processors.settings',)
     8#
     9# Remember, you need to use RequestContext instead of Context in your views:
     10# from django.template import RequestContext
     11# return render_to_response('my_app/my_template.html', {'some_var': 'foo'},
     12#                           context_instance=RequestContext(request))
     13
     14from django.views.debug import get_safe_settings
     15
     16class SafeSettings:
     17    def __init__(self):
     18        self._settings = None
     19   
     20    def __getattr__(self, name):
     21        # Lazy load of settings.
     22        if self._settings is None:
     23            self._settings = get_safe_settings()
     24        # get_safe_settings only returns upper case settings, so let's not worry
     25        # about case sensitivity.
     26        name = name.upper()
     27        try:
     28            return self._settings[name]
     29        except KeyError:
     30            # This method should return the (computed) attribute value or raise
     31            # an AttributeError exception.
     32            raise AttributeError
     33       
     34def settings(request):
     35    return {'settings': SafeSettings()}
     36}}}
Back to Top