Django: How to show data record into Templates without render from Views?
-
05-07-2019 - |
Question
I have a file called header.html and it is included by base.html. In header.html, I have a list of Categories, which are stored in the database. Now, I want to give that list to header.html. The problem is that no function is known to render the data into this file. So how do I do now. Heartfelt thanks!
Solution
You need a template tag - specifically, an inclusion tag. This will render a template with a custom context, in your case the list of categories.
OTHER TIPS
If you want to make variables available in all templates without specifically passing them from a view you can use a Template Context Processor
That will populate your RequestContext, available in the template.
I also suggest you look at Template Inheritence to build the relationships between templates instead of including one within the other.