본문 바로가기
웹 개발 이야기/django

[django] pagination

by Gommin 2023. 5. 9.

// bootstrap5의 bootstrap_pagination 이용
// 아래의 예제는 공지사항 리스트 예제이다.

views.py

def notice_list(request):
    if request.GET.get('page') is None:
        cur_pg = 1
    else:
        cur_pg = request.GET.get('page')

    datalist = Notice.objects.all().order_by('-id')
    datalistcnt = Notice.objects.all().order_by('-id').count()

    paginator = Paginator(datalist, 10)
    
    # url에 있는 현재 page값 get_page로 전달
    page = paginator.get_page(cur_pg)

    context = {
        'page': page,
        'paginator': paginator,
        'cur_pg': cur_pg,
        'max_page': page.paginator.num_pages,
        'datalistcnt': datalistcnt,
    }
    return render(request, 'laboratory/notice.html', context)

 

laboratory/templates/laboratory/notice.html

{% extends 'laboratory/base.html' %}
{% load django_bootstrap5 %}
{% load static %}
{% block title %}
모두의연구소
{% endblock %}

{% block container %}

<div class="container-fluid px-0 w-100">
    <div class="idx_pg sub_pg ">
        <div class="container">
            <div class="breadcrumbs">
                <ul class="d-flex pt_60">
                    <li class="mr_14"><a href="{% url 'laboratory:mainpage' %}" class="fs_14 fw_400 text-light2"><img
                            src="{% static '/img/ico_bread_home.svg' %}" alt="" class="mr-2"> 홈</a></li>
                    <li class="mr_14"><img src="{% static '/img/ico_bread_arrow.svg' %}" alt=""></li>
                    <li class="mr_14"><a class="fs_14 fw_400 text-light2">공지사항</a></li>
                </ul>
            </div>
            <p class="tit_h2 text-primary mt_95">공지사항</p>
            <div class="row mt_46 w-100 mx-auto notice_wrap">
                <div class="col-12 notice_cont px-0">
                    <table class="notice_table w-100">
                        <thead>
                        <tr>
                            <th class="tg-t31z fs_16 fw_400 text-secondary num">번호</th>
                            <th class="tg-t31z text-left pl_20 text-secondary">제목</th>
                            <th class="tg-t31z text-secondary num">게시일</th>
                        </tr>
                        </thead>
                        <tbody>

                        {% for nt in page.object_list %}
                        <tr>
                            <td class="tg-wman fs_14 fw_400 num">{{forloop.counter}}</td>
                            <td class="tg-wman text-left fs_16 fw_600 pl_20 line_h1_5">
                                <a href="{{ nt.get_detail_url1 }}?page={{cur_pg}}">
                                    {{nt.psubject}}
                                </a>
                            </td>
                            <td class="tg-wman fs_14 fw_400 num">{{nt.created_at|date:'y.m.d'}}</td>
                        </tr>
                        {% endfor %}

                        </tbody>
                    </table>
                </div>

                {% if datalistcnt < 1 %}
                <div style="width:100%; text-align:center;">
                    자료가 없습니다.
                </div>
                {% endif %}

            </div>

            {% if max_page > 1 %}
            <div class="mx-auto">
                {% bootstrap_pagination page url=request.get_full_path %}
            </div>
            {% endif %}

        </div>
    </div>
</div>

{% endblock %}

댓글