[Solved]-How can I pass a standard, static variable from all views in Django?


There are a few possible solutions to your problem.

If you really want to have this on every page on your site a context processor is probably your best choice. Context processors are basic way to inject data into all template contexts. Be aware however that the context processor will be called on every request.

An alternative solution would be to create a custom template tag and use it on a shared base template for all of the pages you wish to have your sidebar. Template tags are a bit more complex to create but they are more flexible.

With either solution you should also look at Django’s cache framework. The cache framework makes it pretty easy to temporarily store your calculated values for a while to save some work on each request.





Django’s template inheritance should cover this. You could create a base template that handles your sidebar functionality. Your other views extend this template.

Template Inheritance:



A combination of custom template tags as mentioned previously and template fragment caching should do the trick.

Leave a comment