Django,全局模板变量 [英] Django, global template variables

查看:117
本文介绍了Django,全局模板变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的模板文件(base.html),并且每个其他模板都扩展到它,并使用其块生成内容。某些变量,如nav_obj,用于基本模板文件。



查看:

  nav_obj = NavigationObject.objects.all()

基本模板:

  {%for nav_obj%中的对象} 
< a href ={{object.link}}> { {object.title}}< / a>
{%endfor%}

目前,我需要在每个视图中传递nav_obj 。有没有办法自动发送?

解决方案

编写自己的上下文处理器


I have a base template file (base.html) and every other template extends to it and generates content using its blocks. Certain variables, such as nav_obj, are used in the base template file.

View:

nav_obj = NavigationObject.objects.all()

Base template:

{% for object in nav_obj %}
<a href="{{ object.link }}">{{ object.title }}</a>
{% endfor %}

At the moment, I need to pass nav_obj in every view. Is there any way to have this sent automatically?

解决方案

Write your own context processor.

这篇关于Django,全局模板变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆