| 1 | 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. |
| 2 | |
| 3 | Save 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 | |
| 14 | from django.views.debug import get_safe_settings |
| 15 | |
| 16 | class 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 | |
| 34 | def settings(request): |
| 35 | return {'settings': SafeSettings()} |
| 36 | }}} |