如何将循环应用于此div

时间:2012-07-20 10:03:38

标签: django-templates

因为django模板中没有模数(%)所以如何应用 在这个循环

{% for story in data %}
   {{forloop.counter}}

当forloop.counter%4 == 1时,应该执行此操作         

    <div class="thumb">
    <a href="#"><img src="{{ STATIC_URL }}images/thumb.jpg" width="185" height="185" /></a>
    </div>
    <div class="thumbFooter">
    <span class="view">
    <a href="#" class="viewIcon">{{ story.views }}</a>
    </span>
    <span class="like">
    <a href="#" class="likeIcon">{{ story.likes }}</a>
    </span>
    </div>

    </li>

当forloop.counter%4 == 2或3时,应该执行

  •     <li>
        <div class="thumb">
        <a href="#"><img src="{{ STATIC_URL }}images/thumb.jpg" width="185" height="185" /></a>
        </div>
        <div class="thumbFooter">
        <span class="view">
        <a href="#" class="viewIcon">{{ story.views }}</a>
        </span>
        <span class="like">
        <a href="#" class="likeIcon">{{ story.likes }}</a>
        </span>
        </div>
        </li>
    

    当for循环的forloop.counter%4 == 0时,应该执行

        <li class="omega">
        <div class="thumb">
        <a href="#"><img src="{{ STATIC_URL }}images/thumb.jpg" width="185" height="185" /></a>
        </div>
        <div class="thumbFooter">
        <span class="view">
        <a href="#" class="viewIcon">{{ story.views }}</a>
        </span>
        <span class="like">
        <a href="#" class="likeIcon">{{ story.likes }}</a>
        </span>
        </div>
        </li>
    
        <div class="clear"></div>
    
       {% endfor %}
    
  • 3 个答案:

    答案 0 :(得分:5)

    您可以构建自己的自定义过滤器

    在Django的templatetags目录中,添加一个名为“mod.py”的文件。在该文件中添加以下代码:

    from django import template
    register = template.Library()
    
    def mod(value, arg):
        if value % arg == 0:
            return True
        else:
            return False
    
    register.filter('mod', mod)
    

    在你的模板中使用mod过滤器:

    ...
    {% load mod %}
    ...
    <tr bgcolor="{% if forloop.counter|mod:2 %}#cccccc{% else %}#ffffff">
    ... 
    

    答案 1 :(得分:3)

    我认为您希望class='omega' li每{4}} forloop。以这种方式在模板中使用django Cycle

    {% for story in data %}
      <li {% cycle '' '' '' 'class="omega"' %}>
        <div class="thumb">
        <a href="#"><img src="{{ STATIC_URL }}images/thumb.jpg" width="185" height="185" /></a>
        </div>
        <div class="thumbFooter">
        <span class="view">
        <a href="#" class="viewIcon">{{ story.views }}</a>
        </span>
        <span class="like">
        <a href="#" class="likeIcon">{{ story.likes }}</a>
        </span>
        </div>
      </li>
    
        {% cycle '' '' '' '<div class="clear"></div>' %}
    
    {% endfor %}
    

    答案 2 :(得分:2)

    或者您可以使用divisibleby过滤器。您还可以使用add过滤器调整所有条件:

      

    当forloop.counter%4 == 1时,应执行此操作

    {% if forloop.counter|add:"-1"|divisibleby:"4" %}
    
      

    当forloop.counter%4 == 2或3时,这个

    {% if forloop.counter|add:"-2"|divisibleby:"4" or forloop.counter|add:"-3"|divisibleby:"4" %}
    

    并且不使用add表示可被4整除的那些:

      

    当for循环的forloop.counter%4 == 0时,应该执行

    {% if forloop.counter|divisibleby:"4" %}