I have a common data {the logged-in user’s message number) to display on every page. I can simply pass to template as
dict={'messagenumber':5}
return render_to_response('template.html',dict,context_instance=RequestContext(request))
But it looks tedious if i have this dict data pass to very page. Is there an easier way to pass common data to every page?
Thanks
It blows me away how common this is. You want to use context processors my friend!
Very easy to create, like so:
Since messagenumber is a dynamic variable based on the User, you could pull data from the database by fetching from
request.useras you have full access torequestwithin each context processor.Then, add that to your
TEMPLATE_CONTEXT_PROCESSORSwithin settings.py and you’re all set 🙂 You can do any form of database operation or other logic within the context processor as well, try it out!