【问题标题】:How to create a film strip in django using bootstrap and for loop?如何使用引导程序和 for 循环在 django 中创建电影片段?
【发布时间】:2013-06-24 13:34:49
【问题描述】:

如何在我的模板中显示一组 6 个缩略图来创建幻灯片。我正在通过 for 循环迭代的列表有大约 30 个项目,我想将其分成 6 个块并将其显示为幻灯片滑块。我尝试在 for 循环中使用 range 函数,但这不起作用

<div class="carousel-inner">
        <div class="item active">
            <ul class="thumbnails">

              {% for show in object_list %}
                    <li class="span2">
                        <div class="thumbnail">
                             <a href="{{ show.get_absolute_url }}" data-title="{{ show.name }}" >
                                    {% if show.images.exists %}
                                        {% with show.images.all.0.image as show_image %}
                                            <img src="{% thumbnail show_image 160x160 crop %}" alt="{{show.name}}" class="thumbnail">
                                        {% endwith %}
                                    {% else %}
                                        <img src="{% static 'img/default-image.gif' %}" alt="{{show.name}}" class="thumbnail">
                                    {% endif %}
                                </a>


                        </div>
                    </li>
            {%endfor%}  
           </ul>
        </div>

【问题讨论】:

  • 每 6 张图片一个轮播?

标签: django twitter-bootstrap django-templates


【解决方案1】:

如果您希望每 6 张图片有 1 个轮播,那么您可以这样做

步骤 1) 在您的模板文件夹中创建一个名为 film-slider.html 的新 .html 文件。

{% for reel in reels %}
<div class="carousel-inner">
    <div class="item active">
        <ul class="thumbnails">
            {% for show in reel %}
            <li class="span2">
                <div class="thumbnail">
                     <a href="{{ show.get_absolute_url }}" data-title="{{ show.name }}" >
                        {% if show.images.exists %}
                            {% with show.images.all.0.image as show_image %}
                                <img src="{% thumbnail show_image 160x160 crop %}" alt="{{ show.name }}" class="thumbnail">
                            {% endwith %}
                        {% else %}
                            <img src="{% static 'img/default-image.gif' %}" alt="{{ show.name }}" class="thumbnail">
                        {% endif %}
                    </a>
                </div>
            </li>
            {% endfor %}  
       </ul>
    </div>
</div>
{% endfor %}

第 2 步) 在你的 templatetags/tags.py 中(如果你还没有创建它)

from django import template

register = template.Library()

def filmslider(reel):
    reels = []
    for i in range(0, len(reel), 6):
        reels.append(reel[i:i+6])
    return {'reels':reels}

register.inclusion_tag('film-slider.html')(filmslider)

这将在您通过{% load tags %} 加载模板后生成包含标签。

这将使这项工作像 {% filmslider object_list %} 一样为您工作,您将替换 发布的所有上述 html 代码。

我还没有测试过,但它应该可以工作,如果将来你想为这个标签提供更多功能,你可以简单地在标签定义中添加参数,我会举个例子.

def filmslider(reel, slides):
    #do the code.

这将引导您到 {% filmslider object_list 9 %} 瞧,现在您可以将电影卷轴从 6 张幻灯片扩展到 9 张。

希望这会有所帮助!

【讨论】:

  • 非常感谢,这太棒了。另外,我是 Django 新手,您能告诉我应该在哪里创建我的 tempatetags 目录吗?
  • 在您的应用模块中创建一个名为 templatetags 的文件夹,并确保其中包含 init.py。它在文档中,google templatetags Django 和它的第一个结果! :)
  • 绝对是我想要的,这也将对我未来的前端演示有所帮助。非常感谢!
  • 我现在收到一个错误 - TemplateSyntaxError at / 'tagswoko' is not a valid tag library: Template library tagswoko not found, try
  • 你把你的文件命名为 tagswoko 了吗?
猜你喜欢
  • 2021-02-25
  • 1970-01-01
  • 2019-11-18
  • 2021-04-23
  • 1970-01-01
  • 2019-09-11
  • 2016-07-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多