2012-12-12 2 views
0

일부 요소 목록을 인쇄하는 배열이 있습니다. '4'라고 말한 그룹에서 그 요소를 인쇄하고 싶습니다. 배열에 요소가 10 개있는 경우입니다. 다음 내 템플릿 먼저 <div> 첫 번째 요소를 보여주고 다음 <div> 다음 네 요소를 보여줍니다. 등등.배열의 요소를 django에 인쇄 할 때 문제가 있습니까?

나는 PHP에서 인쇄하는 것처럼 인쇄하려고했지만 여기서는 작동하지 않으므로 그렇게 할 방법을 제안 해주십시오.

는 c.list 9 개 제품이 있고 내가 위에서 언급 한대로 나는 그들에게 보여주고 싶은 :

{% if c.list|length >= 1 or c.list|length < 5 %} 
     {% for p in c.list %} 

     <div class="dis_box1"> 

     <div class="item_imagebox_01"><a href="/shop/product/{{p.title}}"><img style ="width:145px;height:190px;"alt="" src="{{ MEDIA_URL }}{{p.image}}"></a> 
     <div class="img_line1"></div> 
     </div> 

     <div class="left"><span class="heart_text1"><a href="/shop/product/jhgjgj/">{{p.title}}</a></span></div> 

      </div> 

     {% endfor %} 
{% endif %} 
+1

시도한 코드를 볼 수 있습니까? 귀하의 질문은 해석하기가 어렵습니다. 템플릿의 내부에'if'와'for' 태그를 넣고 싶다고 가정합니다. –

+0

제 질문을 편집했습니다 –

+2

당신은'{% if forloop.counter | divisibleby : "4"%}

{% endif %} – sneawo

답변

2

이 당신이 정말보기에 일을해야 일의 종류이다. 템플릿에서 다음

list_by_fours = [] 
list_len = len(c.list) 
last_point = 0 
next_point = 4 

while last_point < list_len: 
    if next_point > list_len: 
    next_point = list_len 
    list_by_fours.append(c.list[last_point:next_point]) 
    last_point += 4 
    next_point += 4 

#Make sure you add list_by_fours to the template context 

:

보기에

{% for bucket in list_by_fours %} 
     {% for p in bucket %} 
      ... 
     {% endfor %} 
{% endif %} 

내가 itertools 또는 다른 멋진 트릭이 할 수있는 방법이있을거야,하지만이 깨끗하고 초보자도 쉽게 이해할 수 있습니다.

관련 문제