为什么我无法在 Django 模板中输入此内容?
data|customTag:variable,forloop.parentloop.counter
我只是希望能够将三个或更多参数传递到过滤器中
假设已经存在 for 循环并且变量/过滤器已在其他地方定义
最佳答案
这是不可能的,因为 django template filters根据定义仅接受一个参数:
Custom filters are just Python functions that take one or two arguments:
The value of the variable (input) – not necessarily a string.
The value of the argument – this can have a default value, or be left out altogether.
建议使用解决方法here这可能适合您的用例。
另一种可能的解决方案是将具有 2 个输入参数的标签拆分为两个具有单个标签的标签,并将它们链接在模板中。这取决于过滤器中的逻辑,但可以是一个选项。
关于Django TemplateSyntaxError 无法解析其余部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27831560/