0

Possible Duplicate:
How can I pass data to any template from any view in Django?

I wonder if there's a way to make every view send a variable or include it in the request context to every template automatically? Something like the messages framework?

My problem is that I want a sidebar on every page rendered with information, say the 10 latest profile pages the user has entered. The only other alternative that I can think of is calling get_recent_users at the end of every view and send it to the template as variable, but I don't like that solution because I've got a lot of views. The rendering of it can be easily managed by the base template I use for template inheritance.

Edit: I forgot to mention that the data that I want to access is stored in request.session, if that makes it easier.

Community
  • 1
  • 1
olofom
  • 6,233
  • 11
  • 37
  • 50
  • That is true. I used the template context solution and it was super easy and worked perfect, but when I got back to mark the answer as accepted it was deleted :( – olofom Apr 23 '12 at 11:31
  • Okay ... i restored my answer. – arie Apr 23 '12 at 12:39

2 Answers2

1

Sounds like you are looking for a context processor.

arie
  • 18,737
  • 5
  • 70
  • 76
0

Write your Custom template tag, and use it in any template you want.

Ahsan
  • 11,516
  • 12
  • 52
  • 79
  • I'm sure that would be a good solution but I did it with the TEMPLATE_CONTEXT_PROCESSORS instead. – olofom Apr 23 '12 at 11:33
  • Its easy to do with TEMPLATE_CONTEXT_PROCESSORS, but you can also do the same thing with Template tags. Mostly Template tags used for formatting or processing the content. check this for more details http://stackoverflow.com/a/2791780/534790 – Ahsan Apr 23 '12 at 11:41