下面是我想在切片中传递的模板的代码
<div class="swiper-container swiper-container2">
<div class="swiper-wrapper">
{% for category in smartphone_banners %}
{% for product in category.product_detail_set.reverse|slice:"1:3:-1" %}
<div class="swiper-slide py-2">
<a href="product/{{product.name}}/{{product.id}}" style="text-decoration: none;">
<img src="{{product.imageURL}}" alt="" style="width: 80%; margin: 0 auto;">
<h6 class="mt-1">{{product.name}}</h6>
<p>{{product.price}}</p>
</a>
</div>
{% endfor %}
{% endfor %}
</div>
</div>
我想用以下内容替换slice:"1:3:-1" %}
slice:"1:totalnu:-1" %}
或slice:"1:{{totalnu}}:-1" %}
,
但这两种方法都不起作用
如何传递totalnu
的值?这是我在views.py
中的代码:
for i in smartphone_banners:
for pr in i.product_detail_set.reverse()[1:3:-1]:
print(pr)
print(totalnu)
totalnu = totalnu + 1
print(totalnu)
lists = []
for ifn in smartphone_banners:
for prf in ifn.product_detail_set.reverse()[(totalnu - 2):totalnu:-1]:
print( 'this is the fonal.', prf)
使用给定的一组变量(上下文)调用模板对象的render()方法。
上下文类似于字典。
创建包含变量的上下文,包括来自
views.py
的totalnu你应该把这个
render(request, template, context)
放在你的views.py
在模板中,使用
{{ totalnu }}
传递totalnu的值相关问题 更多 >
编程相关推荐