简体   繁体   中英

How to send data to base.html in django?

I'm looking for a way to abstract a navigation bar in django and include it in base.html; directly or through a template inclusion.

The part where I'm totally lost is, how to append data to the context so the nav bar can get its contents from database.

I'd like to query the data in views.py and make it available for the template so I can build the nav bar.

Thanks.

No you wouldn't. Write a custom template tag instead.

<html>
 ...
<div class="navbar">{% navbar %}</div>
 ...
</html>

If you need to add information that needs to be available to all templates including base.html you should look at the context_processor.py. This can add information to the context so your page have the data aviailable. I found a blog about this here: http://www.b-list.org/weblog/2006/jun/14/django-tips-template-context-processors/

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