【发布时间】:2015-01-07 16:25:18
【问题描述】:
关于查找模板有很多问题,但我找不到与自定义标签相关的任何内容。
我有以下示意图项目结构:
/
|- app1
| |- templatetags
| | |- my_helpers.py
| |- templates
| |- my_helpers
| |- my_tag.html
|- app2
|- templates
|- include
| |- inclusion.html
|- base.html
在my_helpers.py 中定义了一个简单的包含标记:
from django import template
register = template.Library()
@register.inclusion_tag('my_helpers/my_tag.html')
def my_tag(...):
...
app2/templates/base.html 看起来像这样:
{% load my_helpers %}
... some markup ...
{% my_tag %}
{% include 'include/inclusion.html' %}
这里my_tag 工作得很好。但是,当我尝试在 'include/inclusion.html' 中使用它时(我在其中添加了一个 {% load my_helpers %} 标记),它失败了:
django.template.base.TemplateDoesNotExist
TemplateDoesNotExist: my_helpers/my_tag.html
据我了解,它仅在当前应用程序中查找模板。但是为什么它只发生在included 模板上呢?有没有可能让它发挥作用?
【问题讨论】: