Bootstrap Carousel模板中分割Django Queryset [英] Split Django Queryset in template for Bootstrap Carousel
问题描述
我有一个模型横幅
与字段图像
和名称
。我需要这个模型的数据,在一个移动的Bootstrap转盘上以三组显示。
I have a model Banner
with fields image
and name
. I need data from this model to be displayed in groups of three on a moving Bootstrap carousel.
我目前的实现是我能想到的最昂贵和最简单的一个。
My current implementation is the most expensive and simple one I could think of.
banner1 = Banners.objects.filter(id__exact = 1)
重复
模型。
banner1 = Banners.objects.filter(id__exact=1)
repeated
for 9 entries in the Model.
我的问题是可以拆分一个查询器
Banners.objects.all()
进入三组三个条目,然后如何在Bootstrap Carousel的三个不同幻灯片中显示三组?
My question is that is it possible to split one queryset
Banners.objects.all()
into the three groups of three entries and then how would I go about displaying the three groups across three different slides of the Bootstrap Carousel?
推荐答案
p>这似乎是一个使用django可分割过滤器的好地方。由于您需要每三个实例添加额外的分组信息,因此可能看起来像这样(非常抽象地,因为我不知道您的具体模板需求):
This seems like a good place to use the django divisibleby filter. Since you need to add the extra grouping info every three instances, it could look something like this (very abstractly, since I don't know your specific template demands):
{% for banner in banners %}
{% if forloop.counter0|divisibleby:"3" %}
<!-- your carousel wrapping code here -->
{% endif %}
<!-- code for each individual banners here -->
{% if forloop.counter0|divisibleby:"3" %}
<!-- rest of your carousel wrapping code here -->
{% endif %}
其中横幅将是包含 Banners.objects.all()
这篇关于Bootstrap Carousel模板中分割Django Queryset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!