Bootstrap Carousel模板中分割Django Queryset [英] Split Django Queryset in template for Bootstrap Carousel

查看:117
本文介绍了Bootstrap Carousel模板中分割Django Queryset的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型横幅与字段图像名称。我需要这个模型的数据,在一个移动的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆