Use the pager Day 58

1. Django own pager.

Publish_list using the code:

The code generation template functions:

  <div class="col-lg-8 col-lg-offset-4">
                    <nav aria-label="Page navigation">
                        <ul class="pagination">
                            <li><a href="?page=1">首页</a></li>
                            {% if page.has_previous %}
                                <li><a href="?page={{ page.previous_page_number }}">上一页</a></li>
                            {% else %}
                                <Li class = "Disabled" > < A the href = "JavaScript: void (0);" > Previous </ A > </ Li > 
                            {% endif%} 
{# Second way once the number of display change paginator after a small error occurs, then at the rear end restriction page_range then transmitted over the line #} 
{# a defective manner for NUM% in paginator.page_range {%}} # 
                         {%}% for NUM in page_range 
                                {IF current_page% % NUM} == 
                                    < Li class = "Active" > < A the href = "? Page NUM = {{}}" > {{NUM}} </a></li>
                                {% else %}
                                    <li><a href="?page={{ num }}">{{ num }}</a></li>
                                {% endif %}
                            {% endfor %}
                            {% if page.has_next %}
                                 <li><a href="?page={{ page.next_page_number }}">下一页</a></li>
                            {% else %}
                                <li class="disabled"><a href="javascript:void(0);">下一页</a></li>
                            {% endif %}
                            <li><a href="?page={{ paginator.num_pages }}">尾页</a></li>
                        </ul>
                    </nav>
                </div>

 

Code view function:

# Press show 
@login_required
 DEF publish_list (Request):
     # create publish data 
    # inefficient this data is created 
    # for i in the Range (1, 101): 
    #      models.Publish.objects.create (name = "hope Press _% s "% i, city =" _% s Chengdu region "% i, email =" [email protected] ") # low efficiency 
    publish_list = models.Publish.objects.all () 
    the paginator = Paginator ( publish_list, 8 ) 
  # prevent input publish_list first time, did not result in an error page, so with the try and excepttion If not set = 1 then current_page
try : current_page = int (request.GET.get ( " page ")) except Exception as e: current_page = 1 Page = paginator.get_page (current_page) # Get the current page number of all data # To prevent the line after each show the number of piecemeal, too many of paginator.page_range to display on the page, on the line following settings # us page by page display 11 as an example. # If the total number of pages is greater than 11 # Step IF paginator.num_pages> 11 : IF current_page -. 5 <. 1: # When this page is smaller than the intermediate page page_range Range = (. 1, 12 is ) elif current_page. 5 +> paginator.num_pages: # Current when the page is larger than the intermediate page page_range = Range (paginator.num_pages - 10, + paginator.num_pages. 1 ) the else : page_range= range(current_page - 5, current_page + 6) else: page_range = paginator.page_range # 第二步 将对应的模板中的paginator.page_range改成page_range即可, return render(request, "publish_list.html", {"paginator": paginator, "page": page, "current_page": current_page, "page_range": page_range})

 

2. The final version of the template function code is: is book_list used in library management system to achieve. The view function is book_list_base, previously used template inheritance, carry parameters.

< Table class = "Table Table Table-Striped-hover" > 
            < thead > 
            < TR > 
                < TH > ID </ TH > 
                < TH > Title </ TH > 
                < TH > Price </ TH > 
                < TH > Press </ TH > 
                < TH > author </ TH > 
                < TH > published </ TH >
                <th>编辑</th>
            </tr>
            </thead>
            <tbody>
            {% for book in book_list %}
                <tr>
                    <td>{{ forloop.counter }}</td>
                    <td>{{ book.title }}</td>
                    <td>{{ book.price }}</td>
                    <td>{{ book.publish.name }}</td>
                    <td>
                        {% for au in book.authors.all %}
                            {{ au.name }} |
                        {% endfor %}
                    </td>

                    <td>
                        {{ book.pub_date|date:'Y-m-d' }}
                    </td>
                    <td>
                        <button class="btn btn-danger btn-xs del_btn" book_pk="{{ book.pk }}">删除</button>
                        |
                        <button class="btn btn-info btn-xs"><a
                                href="http://127.0.0.1:8000/edit_book_list/?pk={{ book.pk }}">编辑</a></button>
                    </td>
                </tr>

            {% endfor %}

            </tbody>
        </table>
        <div class="col-lg-8 col-lg-offset-4">
            {{ paginator.page_html|safe }}
        </div>
   

 

Html file which paging is to create good code, pass over to achieve through the back end of the class.

View function code:

Utilization of the display section on the line, the final version of the show 
@login_required 
DEF author_book (Request):
     # is_login = request.session.get ( 'is_login') 
    # IF not is_login: 
    # return redirect ( "/ the Login /") 
    author_list = Models. Author.objects.all ()
     # create AuthorDetail data 
    # inefficient to create this data 
    # for I in Range (10, 110): 
    #      models.AuthorDetail.objects.create (random.randrange Gender = (0, 2), tel = 18228069171, addr = " Chengdu", Birthday = "2019-07-18") 
    # Step Author then create data 
    # for I in Range (10, 110): 
    #     models.Author.objects.create(name="芒果鱼_%s" % i, age=random.randrange(10, 30), au_detail_id=i)
    from app01.zhongji import Paginator
    current_page = request.GET.get('page')
    paginator = Paginator(request, current_page, author_list.count(), 8, 11)
    book_list = author_list[paginator.start: paginator.end]

    return render(request, "author_list.html", {"author_list": book_list, "paginator": paginator, "current_page" : Current_page})
     # return the render (Request," author_list.html ", { 'author_list': author_list}) before the tab on the line # not used to show all the return value

 

View function parameters passed.

 

Wherein the class files, zhongji.py Code:

class Paginator:
     DEF  the __init__ (Self, Request, current_page, ALL_COUNT are, per_page = 10, max_page_num = 13 is ):
         "" " 
        Package tab data 
        : param current_page: Current Page 
        : param all_count: Total number of stripes in the database 
        : param per_page : number of stripes displayed per page 
        : param max_page_num: up to displayed page number 
        : param num_pages: by number of the total number of pieces / displayed per page, find the total number of pages 
        "" " 
        the try : 
            current_page = int ( current_page)
         the except Exception AS E: 
            current_page =. 1
         IF current_page <. 1 : 
            current_page= 1
        self.current_page = current_page
        self.all_count = all_count
        self.per_page = per_page

        # 计算总页数
        num_pages, temp = divmod(all_count, per_page)
        if temp:
            num_pages += 1
        self.num_pages = num_pages

        self.max_page_num = max_page_num  # 11
        self.page_count_half = int((self.max_page_num - 1) / 2)  # 5

        import copy
        self.url_args = copy.deepcopy(request.GET)
        print(self.url_args.urlencode())


        """
        self.num_pages=100
        per_page=8

        current_page =1     [0:8]
        current_page =2     [8:16]
        current_page =3     [16:24]
                            [(current_page-1)*per_page:current_page*per_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 the total number of pages is less than self.max_page_num (up display page number) 
        IF self.num_pages <= self.max_page_num: 
            PAGE_START =. 1 
            PAGE_END = +. 1 self.num_pages
         the else :
             # If the current page number <= 11 up to appear on the page / page 2 when 
            IF self.current_page <= self.page_count_half: 
                PAGE_START =. 1 
                PAGE_END = self.max_page_num. 1 +
             # If the current page number is greater than 11/2 + displays a maximum total number of pages 
            elif self.current_page + self.page_count_half> Self. NUM_PAGES: 
                PAGE_START = self.num_pages - self.max_page_num +. 1
                page_end = self.num_pages + 1
            else:
                page_start = self.current_page - self.page_count_half
                page_end = self.current_page + self.page_count_half + 1

        page_html_list = []

        # 首页
        self.url_args['page'] = 1
        first_page = '<nav aria-label="Page navigation"><ul class="pagination"><li><a href="?%s">首页</a></li>' % (self.url_args.urlencode())
        page_html_list.the append (FIRST_PAGE) 

        ifPrevious#
         self.current_page <= 1:
            prev_page = '<li class="disabled"><a href="javascript:void(0);">上一页</a></li>'
        else:
            self.url_args['page'] = self.current_page - 1
            prev_page = '<li><a href="?%s">上一页</a></li>' % (self.url_args.urlencode(), )
        page_html_list.append(prev_page)

        # 显示页码
        for i in range(page_start, page_end):
            self.url_args['page'] = i
            if self.current_page == i:
                temp = '<li class="active"><a href="?%s">%s</a></li>' % (self.url_args.urlencode(), i)
            else:
                temp = '<li><a href="?%s">%s</a></li>' % (self.url_args.urlencode(), i)
            page_html_list.append(temp)

        # 下一页
        if self.current_page >= self.num_pages:
            next_page = '<li class="disabled"><a href="javascript:void(0);">下一页</a></li>'
        else:
            self.url_args['page'] = self.current_page + 1
            next_page = '<li><a href="?%s">下一页</a></li>' % (self.url_args.urlencode(), )
        page_html_list.append(next_page)

        # 尾页
        self.url_args['page'] = self.num_pages
        last_page = '<li><a href="?%s">尾页</a></li></ul></nav>' % self.url_args.urlencode()
        page_html_list.append(last_page)

        return "".join(page_html_list)

 

Guess you like

Origin www.cnblogs.com/longerandergou/p/11210333.html