What we could do is to add all this information to the response dictionary. However, it would contradict the DRY principle of Django.
A good approach is to use a customized Context Processor, which will add to the dictionary the returned variables of the function.
Specifically, the modifications we must do can be the following:
- Define the Context Processor function, like for example:
return {'additional_values': ['a','b','c']}
- Load the function inside the TEMPLATE_CONTEXT_PROCESSORS variable of the setting.py:
...
"path_to_context_processor_function.context_processor_function",)
- Use the variables inside the templates like any other variables of the dictionary
http://www.b-list.org/weblog/2006/jun/14/django-tips-template-context-processors/
https://docs.djangoproject.com/en/1.1/ref/settings/#std:setting-TEMPLATE_CONTEXT_PROCESSORS
http://stackoverflow.com/questions/335231/in-django-is-it-possible-to-access-the-current-user-session-from-within-a-custo
Hello Dude,
ReplyDeleteAs it turns out, Django provides an extremely easy way to do this. Every time you render a template, you give it a context, this is a dictionary-like object whose keys are variable names and whose values are the values of the variables. When you render a template with a given context, every key in the context dictionary becomes a variable in the template that you can access and use. Thanks for sharing it.....
Extract Web Data