Reputation: 3545
I have a footer included in my base template with:
{% include "footer.html %}
I need to show it in every page. But the footer has several statics which needs to be calculated with several queries each time the page loads. As far as I know, this include cannot run queries because any views is called. And I don't want to replicate the query for that in all my views, I think it's a dirty solution. What's the best practice in this case? I think it's a quite common problem.
Upvotes: 0
Views: 63
Reputation: 600059
Although context processors are good for including standard things in every template, I suspect for your purposes a custom template tag that renders the entire footer would be a better bet - probably an inclusion tag would do the job.
Upvotes: 2
Reputation: 13496
Another option not mentioned here so far are custom template tags. Depending on what exactly you want to do a context processor might be overkill.
Upvotes: 0
Reputation: 5442
What about creating a Context Processor, and return the query from there ? The result will be available in every view, if that's what you desire ?
Upvotes: 0
Reputation: 10561
You can use context processors to run your queries in all views and update your context with necessary data.
Upvotes: 0
Reputation: 53386
If statistics are global (not something related to specific page/request) you can implement your custom context processor which can calculate statistics and add corresponding variables in the context. That context variables/dict can be used by footer.html
to put the statistics.
Refer Writing custom context processor
Upvotes: 1