简体   繁体   中英

Django Template include a html template, with loaded data

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!

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM