我有一个用例,根据include
标记和传递给它的参数,表单的不同字段会以不同的方式呈现。为了保持它的干燥,有时我希望包含相同的模板并传递不同的参数,比如下面的简化示例:
main_form.html
{% include "./field-snippet.html" with type=list %}
field_snipt.html
{% include "./field-snippet.html" with type="regular" %}
但这给出了以下TemplateSyntaxError
:
The relative path '"./field-snippet.html"' was translated to template name 'myappname/field-snippet.html', the same template in which the tag appears.
或者,我可以为不同类型的字段提供不同的html文件,但似乎有很多文件,每个文件中没有太多代码。关于如何最好地解决这个问题,有什么想法吗?使用自定义Widgets
是这里最好的方法吗(我还没怎么玩)
这与其说是一个答案,不如说是一种变通方法,但使用绝对路径可以避免此错误。
{% include "YourApp/field-snippet.html" with type="regular" %}
我认为开发人员有充分的理由导致这个错误,所以使用的风险自负。文档讨论使用与{% extends %}
相同的名称,但我没有发现任何关于递归{% include %}
的提及。