2011-11-26 221 views
1

four <div>在一個頁面。我想在每個<div>上顯示25記錄。我寫了亂碼。它不工作。你能幫我嗎?謝謝關於for循環(模板循環)

<div one > <div two> <div three> <div four> 
1    26   51   76 
2    27   52   77 
3    28   53   78 
-    -   -   - 
-    -   -   - 
25    50   75   100 
</div>  </div>  </div>  </div> 

這裏是模板代碼。

<div class="fourcolumnswrapper"> 
     <div class="moviescolumn"> 
    {% for movie in movies.object_list %} 
    <a href="{{ movie.get_absolute_url}}">{{ movie.title }}</a><br/> 
    <!-- Display first 25 record in one div --> 
    {% if forloop.counter == 25 %} 

     </div> 
     {% endif %} 
    {% if forloop.counter > 25 and forloop.counter <= 50 %} 
     <div class="moviescolumn"> 

     {% if forloop.counter == 50 %} 
      </div> 
     {% endif %} 
    {% endif %} 
    {% if forloop.counter > 50 and forloop.counter < 75 %} 
     <div class="moviescolumn"> 

     {% if forloop.counter == 75 %} 
      </div> 
     {% endif %} 
    {% endif %} 
    {% endfor %} 


    </div> 

更新輸出:http://i.imgur.com/zuc8y.png這裏是源代碼generated by view sourcehttp://dpaste.org/dxKi8/

+0

你得到什麼樣的輸出? – second

+0

@second http://i.imgur.com/zuc8y.png – Kulbir

+0

代碼比圖片也許更實用...嘗試http://dpaste.com/ – second

回答

1

你做錯了。試試這個

<div class="moviescolumn"> 

    {% for movie in movies.object_list %} 
<a href="{{ movie.get_absolute_url}}">{{ movie.title }}</a><br/> 
<!-- Display first 25 record in one div --> 
{% if forloop.counter == 25 %} 
<!-- Paginator--> 
<!-- End Paginator --> 
</div> 
<div class="moviescolumn"> 
{% endif %} 
{% if forloop.counter == 50 %} 
</div> 
<div class="moviescolumn"> 
{% endif %} 
{% if forloop.counter == 75 and forloop.counter < 100 %} 
</div> 
{% endif %} 
{% endfor %} 
0

你可能想輸出是這樣的:

<div class="fourcolumnswrapper"> 
    <div class="moviescolumn"> 
    <a href="movie_link.html">Movie title here 1</a><br /> 
    <a href="movie_link.html">Movie title here 2</a><br /> 
    <a href="movie_link.html">Movie title here 3</a><br /> 
    <a href="movie_link.html">Movie title here 4</a><br /> 
    <a href="movie_link.html">Movie title here 5</a><br /> 
    </div> 
    <div class="moviescolumn"> 
    <a href="movie_link.html">Movie title here 6</a><br /> 
    <a href="movie_link.html">Movie title here 7</a><br /> 
    <a href="movie_link.html">Movie title here 8</a><br /> 
    <a href="movie_link.html">Movie title here 9</a><br /> 
    <a href="movie_link.html">Movie title here 10</a><br /> 
    </div> 
    <div class="moviescolumn"> 
    <a href="movie_link.html">Movie title here 11</a><br /> 
    <a href="movie_link.html">Movie title here 12</a><br /> 
    <a href="movie_link.html">Movie title here 13</a><br /> 
    <a href="movie_link.html">Movie title here 14</a><br /> 
    <a href="movie_link.html">Movie title here 15</a><br /> 
    </div> 
    <div class="moviescolumn"> 
    <a href="movie_link.html">Movie title here 16</a><br /> 
    <a href="movie_link.html">Movie title here 17</a><br /> 
    <a href="movie_link.html">Movie title here 18</a><br /> 
    <a href="movie_link.html">Movie title here 19</a><br /> 
    <a href="movie_link.html">Movie title here 20</a><br /> 
    </div> 
</div> 

fourcolumnswrapper爲四列顯示的div,你需要這樣的CSS:

.fourcolumnswrapper { 
    width:1000px; /* width must be set */ 
} 

.moviescolumn { 
    float:left; 
    width: 25%; 
} 

最後在Django模板系統來實現這一目標你可以這樣寫:

<div class="fourcolumnswrapper"> 
    <div class="moviescolumn"> 
    {% for movie in movies.object_list %} 
    <a href="{{ movie.get_absolute_url}}">{{ movie.title }}</a><br/> 
    {% if forloop.counter|divisibleby:"25" and not forloop.last %} 
    </div> 
    <div class="moviescolumn"> 
    {% endif %} 
    {% endfor %} 
    </div> 
</div> 

你可以看到最後的效果here on JSFiddle

1

爲什麼您使用的表格數據的DIV?這是一個非常好的TABLE元素。

+0

我不知道如何輕鬆完成HTML表格和CSS2 - 我的意思是行作爲列:'1 | 26 | 51 | 76'不是'1 | 2 | 3 | 4'等 – Xaerxess