Django:嵌套自定义模板标签
我在 Django 中编写了 2 个自定义模板标签。 目标是在另一个自定义标签中使用一个自定义标签。允许吗? 自定义“外部”标签的模板如下所示:
<ul>
{% for type in types %}
{% custom_internal_tag param1 %}
{% endfor %}
</ul>
渲染后的结果是
无效的块标记:“custom_internal_tag”,应为“empty”或“endfor”
是否允许嵌套自定义标签?出现这样的错误的原因是什么?
I have written 2 custom template tags in Django.
The goal is to use one custom tag inside of another. Is it permitted?
The template for custom "outer" tag looks like this:
<ul>
{% for type in types %}
{% custom_internal_tag param1 %}
{% endfor %}
</ul>
Which after rendering results in
Invalid block tag: 'custom_internal_tag', expected 'empty' or 'endfor'
Are nested custom tags allowed? What would be the cause of such an error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它们绝对是允许的,但由于您的自定义标签未正确定义、定位或加载,因此可能会出现该错误。
确保所有自定义标签都位于
app/templatetags
目录中,并且通常使用@register
装饰器正确加载。参考: https ://docs.djangoproject.com/en/dev/howto/custom-template-tags/#registering-custom-filters
They are definitely allowed, but that error can come up because your custom tag is not properly defined, located or loaded.
Make sure all your custom tags are located in your
app/templatetags
directory and are loaded properly, usually using the@register
decorator.Reference: https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#registering-custom-filters