2017-06-04 107 views
0

我有一个由Django生成的日历,并使用Bootstrap进行了样式化。 这里是Django模板代码:每四次迭代修改Django循环

<div class="content"> 
    {% for month in period.get_months %} 
    <div class="col-md-3"> 
     <div class="row row-centered"> 
     <button class="btn btn-custom active" href="{% url "month_calendar" calendar.slug %}{% querystring_for_date month.start 2 %}">{{month.name}}</button> 
     </div> 
     <div> 
     {% month_table calendar month "small" %} 
     </div> 
    </div> 
    {% endfor %} 
</div> 

现在,由于月份有不同的数周,他们具有不同的高度,我想避免这样的: enter image description here

我从this answer理解最好的解决方案是使用clearfix

那么,我该如何修改我的模板中的for循环,以便Django每四个项目插入一个额外的行<div class="clearfix"></div>

+1

你见过['divisibleby'(https://docs.djangoproject.com/en/1.10/ref/templates/builtins/#divisibleby)过滤? –

回答

2

Django模板循环存储当前索引forloop.counter变量。你可以在docs阅读。所以,你可以尝试改变这样你的代码:

<div class="content"> 
    {% for month in period.get_months %} 
    <div class="col-md-3"> 
     <div class="row row-centered"> 
     <button class="btn btn-custom active" href="{% url "month_calendar" calendar.slug %}{% querystring_for_date month.start 2 %}">{{month.name}}</button> 
     </div> 
     <div> 
     {% month_table calendar month "small" %} 
     </div> 
    </div> 
    {% if forloop.counter|divisibleby:4 %} 
     <div class="clearfix"></div> 
    {% endif %} 
    {% endfor %} 
</div> 
+0

HI。我完全不熟悉Django的%符号。我尝试了你的代码,这是有道理的,但我得到一个错误'无法解析剩余:%''%'' – user2314737

+0

嗨,我修复了我的答案,现在尝试 –

+0

现在我收到错误消息''endfor ',预计'elif','else'或'endif'。你忘了注册或加载这个标签吗?' – user2314737