관리 메뉴

웹개발자의 기지개

[Django] template tags and filters 짝수,홀수 구분 forloop.counter|divisibleby:2 본문

python/Django

[Django] template tags and filters 짝수,홀수 구분 forloop.counter|divisibleby:2

http://portfolio.wonpaper.net 2022. 4. 7. 22:13

template tag 중에서 forloop.counter 에 대한 사항이다.

for 문으로 돌릴때, 짝수 / 홀수로 구분하는 코딩을 할때 사용할 수 있다.

 

forloop.counter 는 index가 1부터

forloop.counter0 은 index는 0부터 시작

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{% for p in post_list %}
 
                            <!-- Blog post-->
                            <div class="card mb-4">
                                {{ forloop.counter }}<a href="#!"><img class="card-img-top" src="https://dummyimage.com/700x350/dee2e6/6c757d.jpg" alt="..." /></a>
                                <div class="card-body">
                                    <div class="small text-muted">{{ p.created_at }}</div>
                                    <h2 class="card-title h4">{{ p.title }}</h2>
                                    <class="card-text">{{ p.content }}</p>
                                    <class="btn btn-primary" href="{{ p.get_absolute_url }}">Read more →</a>
                                </div>
                            </div>
{% if forloop.counter|divisibleby:2 %}
                         </div>
                        <div class="col-lg-6">
{% endif %}
 
{% endfor %}
cs

 

 

 

 

 

참고 : https://docs.djangoproject.com/en/4.0/ref/templates/builtins/

 

Built-in template tags and filters | Django documentation | Django

Django The web framework for perfectionists with deadlines. Overview Download Documentation News Community Code Issues About ♥ Donate

docs.djangoproject.com

참고 : https://dev-navill.tistory.com/17

Comments