Reputation: 93
how can I implement a Django template (alert-msg.html) which I can include in base.html. Which loads its data.
To inlcude a alert-msg.html page into base.html is not the problem. But how this template loads its data for self. I won't give the data with every view call.
There is the {% loads function_name %}
but this only calls a function... can this function manipulate the alert-msg.html template? To inject the data? This I tried but still without result... :-)
Thanks!
Upvotes: 1
Views: 907
Reputation: 1102
It might also be possible to use the include builtin:
{% include "name_snippet.html" with person="Jane" greeting="Hello" %}
https://docs.djangoproject.com/en/2.0/ref/templates/builtins/#include
Upvotes: 1
Reputation: 12195
It sounds like what you're looking for is an inclusion tag, which is basically like a mini view you can call from your template and pass in vars from the request context, then act upon them in the tag function. You can include that tag in your base.html if you want.
If the data isn't going to be present with every view, you can write your inclusion tag to deal with that, or you can make the data available in every call with a custom context processor.
By the way, {% loads foo %}
isn't for loading a function - it's for loading a non-default template tag library, which you will need to do if you want to use your newly made inclusion tag. I recommend reading all of the custom template tags docs first, to give you some context for when you write the inclusion tag.
Upvotes: 2