Django: exposing settings in templates, the easy way

NOTE: I have submitted a patch to have this functionality as part of Django core. I’m posting this code here in case the patch gets rejected, since similar motions have previously been rejected. The ticket is at http://code.djangoproject.com/ticket/3818, and I will update this post if the patch gets accepted.

I find it very handy to be able to expose parts of settings.py in templates. The Django docs show you how to do this using context processors and give the code for the MEDIA_URL context processor here. However, it seems silly to me to have to write custom processors all the time. Instead, I propose a single processor that exposes a list of settings (stored in settings.py itself) to the template layer.

The gist containing the code is here: http://gist.github.com/155894

Enjoy.

Tags: , ,

2 Responses to “Django: exposing settings in templates, the easy way”

  1. Mike Malone says:

    Another approach is to use the get_safe_settings function from django.views.debug. If you use that function, make sure you look at it’s implementation since it just filters for certain words that make a setting “dangerous” (like “password”). Here’s my context processor for getting settings into templates: http://gist.github.com/156638

    The code for get_safe_settings is here: http://code.djangoproject.com/browser/django/trunk/django/views/debug.py#L13

  2. Thanks for this post. I am new at development and this is a big help.

Leave a Reply