python_fullstack - Django 프레임워크(8) - Pagination 페이지 나누기

Django Pagination


1. 사용자 정의 페이지


1. 기초판 사용자 정의 페이지

data = []

for i in range(1, 302):
    tmp = {"id": i, "name": "yang-{}".format(i)}
    data.append(tmp)

print(data)


def user_list(request):

    # user_list = data[0:10]
    # user_list = data[10:20]
    try:
        current_page = int(request.GET.get("page"))
    except Exception as e:
        current_page = 1

    per_page = 10

    #  
    total_count = len(data)
    #  
    total_page, more = divmod(total_count, per_page)
    if more:
        total_page += 1

    #  
    max_show = 11
    half_show = int((max_show-1)/2)

    if current_page <= half_show:
        show_start = 1
        show_end = max_show
    else:
        if current_page + half_show >= total_page:
            show_start = total_page - max_show
            show_end = total_page
        else:
            show_start = current_page - half_show
            show_end = current_page + half_show

    #  
    data_start = (current_page - 1) * per_page
    data_end = current_page * per_page

    user_list = data[data_start:data_end]

    #  
    page_html_list = []
    #  
    first_li = '
  • 첫 페이지
  • '
    page_html_list.append(first_li) # if current_page == 1: prev_li = '
  • 이전 페이지
  • '
    else: prev_li = '
  • 이전 페이지
  • '
    .format(current_page - 1) page_html_list.append(prev_li) for i in range(show_start, show_end+1): if i == current_page: li_tag = '
  • {0}
  • '
    .format(i) else: li_tag = '
  • {0}
  • '
    .format(i) page_html_list.append(li_tag) # if current_page == total_page: next_li = '
  • 다음 페이지
  • '
    else: next_li = '
  • 다음 페이지
  • '
    .format(current_page+1) page_html_list.append(next_li) # page_end_li = '
  • 끝 페이지
  • '
    .format(total_page) page_html_list.append(page_end_li) page_html = "".join(page_html_list) return render(request, "user_list.html", {"user_list": user_list, "page_html": page_html})

    2, 포장 재사용 가능 버전

    class Pagination(object):
        def __init__(self, current_page, total_count, base_url, per_page=10, max_show=11):
            """
            :param current_page:  
            :param total_count:  
            :param per_page:  
            :param max_show:  
            """
            try:
                current_page = int(current_page)
            except Exception as e:
                current_page = 1
    
            self.current_page = current_page
            self.total_count = total_count
            self.base_url = base_url
            self.per_page = per_page
            self.max_show = max_show
    
            #  
            total_page, more = divmod(total_count, per_page)
            if more:
                total_page += 1
    
            half_show = int((max_show - 1) / 2)
            self.half_show = half_show
            self.total_page = total_page
    
        @property
        def start(self):
            return (self.current_page - 1) * self.per_page
    
        @property
        def end(self):
            return self.current_page * self.per_page
    
        def page_html(self):
    
            if self.current_page <= self.half_show:
                show_start = 1
                show_end = self.max_show
            else:
                if self.current_page + self.half_show >= self.total_page:
                    show_start = self.total_page - self.max_show
                    show_end = self.total_page
                else:
                    show_start = self.current_page - self.half_show
                    show_end = self.current_page + self.half_show
    
                    #  
            page_html_list = []
            #  
            first_li = '
  • 첫 페이지
  • '
    .format(self.base_url) page_html_list.append(first_li) # if self.current_page == 1: prev_li = '
  • 이전 페이지
  • '
    else: prev_li = '
  • 이전 페이지
  • '
    .format(self.base_url, self.current_page - 1) page_html_list.append(prev_li) for i in range(show_start, show_end + 1): if i == self.current_page: li_tag = '
  • {1}
  • '
    .format(self.base_url, i) else: li_tag = '
  • {1}
  • '
    .format(self.base_url, i) page_html_list.append(li_tag) # if self.current_page == self.total_page: next_li = '
  • 다음 페이지
  • '
    else: next_li = '
  • 다음 페이지
  • '
    .format(self.base_url, self.current_page + 1) page_html_list.append(next_li) # page_end_li = '
  • 끝 페이지
  • '
    .format(self.base_url, self.total_page) page_html_list.append(page_end_li) return "".join(page_html_list)

    3、포장 재사용 버전views.py 사용 방법

    def user_list(request):
        pager = Pagination(request.GET.get("page"), len(data), request.path_info)
        user_list = data[pager.start:pager.end]
        page_html = pager.page_html()
        return render(request, "user_list.html", {"user_list": user_list, "page_html": page_html})

    2. Django 내장 페이지


    1. 내장 페이지 나누기views.py 부분

    from django.shortcuts import render
    from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
    
    L = []
    for i in range(999):
        L.append(i)
    
    def index(request):
        current_page = request.GET.get('p')
    
        paginator = Paginator(L, 10)
        # per_page:  
        # count:     
        # num_pages: 
        # page_range: , : (1,10),(1,200)
        # page:     page 
        try:
            posts = paginator.page(current_page)
            # has_next               
            # next_page_number       
            # has_previous           
            # previous_page_number   
            # object_list            
            # number                 
            # paginator             paginator 
        except PageNotAnInteger:
            posts = paginator.page(1)
        except EmptyPage:
            posts = paginator.page(paginator.num_pages)
        return render(request, 'index.html', {'posts': posts})
    

    2, 내장 페이지 HTML 섹션

    
    
    "en">
        "UTF-8">
        
    
    
    <ul>
        {% <span class="hljs-keyword">for</span> item <span class="hljs-keyword">in</span> posts %}
            <li>{{ item }}</li>
        {% endfor %}
    </ul>
    
    <div class="hljs-class"><span class="hljs-keyword">class</span>="<span class="hljs-title">pagination</span>">
          <<span class="hljs-title">span</span> <span class="hljs-title">class</span>="<span class="hljs-title">step</span>-<span class="hljs-title">links</span>">
            {% <span class="hljs-title">if</span> <span class="hljs-title">posts</span>.<span class="hljs-title">has_previous</span> %}
                <<span class="hljs-title">a</span> <span class="hljs-title">href</span>="?<span class="hljs-title">p</span>={{ <span class="hljs-title">posts</span>.<span class="hljs-title">previous_page_number</span> }}"><span class="hljs-title">Previous</span><span class="hljs-title">a</span>>
            {% <span class="hljs-title">endif</span> %}
              <<span class="hljs-title">span</span> <span class="hljs-title">class</span>="<span class="hljs-title">current</span>">
                <span class="hljs-title">Page</span> {{ <span class="hljs-title">posts</span>.<span class="hljs-title">number</span> }} <span class="hljs-title">of</span> {{ <span class="hljs-title">posts</span>.<span class="hljs-title">paginator</span>.<span class="hljs-title">num_pages</span> }}.
              <span class="hljs-title">span</span>>
              {% <span class="hljs-title">if</span> <span class="hljs-title">posts</span>.<span class="hljs-title">has_next</span> %}
                  <<span class="hljs-title">a</span> <span class="hljs-title">href</span>="?<span class="hljs-title">p</span>={{ <span class="hljs-title">posts</span>.<span class="hljs-title">next_page_number</span> }}"><span class="hljs-title">Next</span><span class="hljs-title">a</span>>
              {% <span class="hljs-title">endif</span> %}
          <span class="hljs-title">span</span>>
    
    <span class="hljs-title">div</span>>
    <span class="hljs-title">body</span>>
    <span class="hljs-title">html</span>>
     
     </div> 
    </code></pre></div>
                                </div>
                            </div>
                        </div>
                        <!--PC WAP -->
                         
                        <script type="text/javascript" src="/views/front/js/chanyan.js"/>
                        <!--  -    -->

    좋은 웹페이지 즐겨찾기