【发布时间】:2019-12-20 10:31:22
【问题描述】:
我正在尝试使用 for 循环重复呈现一段 HTML 代码。但是当我重新加载浏览器时,Django 会抛出 TemplateSyntaxError
<div class="carousel-item active">
{% for number in range(3) %}
<!--Slide {{ number + 1 }}-->
<div class="row">
{% for number in range(6) %}
<!--Slide 1 Col {{ number + 1 }}-->
<div class="col-lg-2">
<div class="card" style="width: 100%;">
<img class="card-img-top" src="..." alt="Card image cap">
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
<a href="#" class="btn btn-primary">Go somewhere</a>
</div>
</div>
</div>
{% endfor %}
</div>
{% endfor %}
</div>
我希望在 for 循环内重复渲染块,但在 / 处得到“TemplateSyntaxError” 无法解析余数:来自“range(3)”的“(3)”
【问题讨论】:
-
您不能进行函数调用(在模板中使用参数),所以
range(3)将不起作用。将其通过上下文传递给模板,或使用 Jina。