请教下 Django 模板继承的一个问题

2015-08-17 18:49:04 +08:00
 le0rn0

比如父模板之中存在全站的导航,每个页面需要这些导航,

而这个父模板中的导航内容是从数据库中读取的,

也就是说每次渲染继承它的模板都要传一个 list 的参数(导航列表),

感觉走了弯路呀,请问有其他方法来实现吗?

1877 次点击
所在节点    问与答
8 条回复
delo
2015-08-17 19:44:55 +08:00
guoqiao
2015-08-17 20:11:37 +08:00
楼上正解
virusdefender
2015-08-17 20:34:39 +08:00
我是自己写了一个 template tag 实现的
le0rn0
2015-08-17 20:36:55 +08:00
@delo 感谢,请问下 Jinjja 模板也能实现吗
le0rn0
2015-08-17 20:37:11 +08:00
@virusdefender 能否详细说下?谢啦
virusdefender
2015-08-17 22:13:07 +08:00
@le0rn0 你不就是需要一个类似全局变量的东西么~

# coding=utf-8
from django import template
register = template.Library ()


@register.simple_tag
def show_website_info (name ):
return {"website_name": "xxx", "website_footer": u"xxx"}[name]

然后在模板里面 {% show_website_info "website_name" %} {% show_website_info "website_footer" %}

https://docs.djangoproject.com/en/1.8/howto/custom-template-tags/
glasslion
2015-08-17 22:20:15 +08:00
context processor 正解
le0rn0
2015-08-17 22:22:08 +08:00
@virusdefender 多谢!就是这种类似全局变量的东西


@delo
@guoqiao
那文档没太看懂哎

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/213860

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX