[Answered ]-Django – Pre render signal?

1👍

Agreeing with Daniel that process_template_response is the hook you’re looking for. I don’t know how many settings you have, but it could also make sense to retrieve all settings once, and store them in some dictionary like structure, so you can access them anytime without hitting the database. You could retrieve them once either per request, or – even if your settings do barely change – when django initializes. You could additionally use django’s signals to update your cached settings on delete or update.

If you have a look at django-dbsettings you will see it does a similar thing!

1👍

Django 1.3 includes the new TemplateResponse class, which allows you to modify the response later in the rendering process – eg in middleware. This looks like it might do what you want.

0👍

If you wish to stick to a scheme similar to what you already have, I’d implement a filter that takes in a list of keys and returns a dict with all the relevant values.

Example filter:

def get_settings(key_list):
    # take values from conf.settings. In practice, can be from any source
    return dict((k, getattr(settings, k, None)) for k in key_list.split(","))
register.filter("get_settings", get_settings)

Usage:

{# load multiple values with a single call #}
{% with "KEY1,KEY2"|get_settings as settings %}
     setting values {{ settings.KEY1 }} and {{ settings.KEY2 }}
{% endwith %}

Note that I used a filter instead of a tag because the with builtin expects a single value before as.

Leave a comment