【发布时间】:2020-05-11 10:49:22
【问题描述】:
我正在尝试使用 for 循环在 django 中通过添加产品范围来循环产品列表{more so a queryset} 以下代码在 cmd 提示符下打印时有效
for card in allproduct[nextRangePage:limit]:
print(card.name)
但是 Jinja 模式由于某种原因失败了
{% for card in product[{%'nextRangePage'%}:{%'limit'%}] %}
错误是
django.template.exceptions.TemplateSyntaxError: 无法解析剩余部分:'[{%'nextRangePage'' from 'allproduct[{%'nextRangePage''
上下文变量是:
context = {
'product': product_list,
'pages': pages,
'nextRangePage': nextRangePage,
}
[product_list = allproduct]
【问题讨论】:
-
试试:
{% for card in allproduct | slice:limit %}或{% for card in allproduct | slice(limit) %} -
它没有用,给出一个错误 'for x in y': %s" % token.contents) django.template.exceptions.TemplateSyntaxError: 'for' 语句应该使用format 'for x in y': for card in allproduct | slice:limit