flask distal tab display

# Flask the front page display

1. Sort principle

There are three ways when querying large amounts of data and display the web:

  • All inquiries, screened at end view / client / paging from the database; most of the recording can not cope with the situation, generally do not use;
  • Pagination query, a query data in each database query is determined according to the specific request of pages; the number of database queries more;
  • Also paged, but a query multiple pages (such as 10), to the client 1, implemented in view; the database requests in terms of reduction than the second method, but the server overhead will be larger;

2. realization

Paging divided into two parts from the functional point:

  • Get the current page and add content to html templates;
  • Configuration 'previous' after an' other html page content and added to the template;

Generally implemented using paging class, it can be customized;
of course, a frame, FlaskSQLAlchem there Pagination type object. Query object calls paginate a method to obtain Pagination object.

2.1 Paging class

Paging class the following functions:

  1. Determine the current page record of the serial number, such as 20 records per page, page 5, the page record id should be 81-100;
  2. Html page generated block code is realized by a method page_html;
The urllib.parse Import urlencode from, quote, unquote 
class Pagination (Object): 
    "" " 
    custom paging 
    " "" 
    DEF the __init __ (Self, current_page, TOTAL_COUNT, the base_url, the params, per_page_count = 10, max_pager_count = 10): 
        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 = 
        total number of data # 
        self.total_count = TOTAL_COUNT 

        # 10 per data 
        self.per_page_count = per_page_count 

        # the maximum page number of the page should be displayed
        max_page_num, divmod = div (TOTAL_COUNT, per_page_count) 
        # self.params [Page] =. 8
        div IF: 
            max_page_num + =. 1 
        self.max_page_num = max_page_num 

        default on page # 11 display the page number (in the middle of this page) 
        self.max_pager_count = max_pager_count 
        self.half_max_pager_count = int ((max_pager_count -. 1) / 2) 

        # the URL prefix 
        self. = the base_url the base_url 

        # `` request.GET`` 
        Import Copy 
        the params = copy.deepcopy (the params) 
        # params._mutable = True 
        get_dict params.to_dict = () 
        # page containing the current list of all the search / search condition 
        # {source: [2,] , Status: [2], Gender: [2], Consultant: [. 1], Page: [. 1]} 
        # Source & Status = 2 = 2 = 2 & Gender & Consultant = =. 8. 1 & Page
        # self.params.urlencode()
        # href="/hosts/?source=2&status=2&gender=2&consultant=1&page=8"
        # href="%s?%s" %(self.base_url,self.params.urlencode())
        self.params = get_dict

    @property
    def start(self):
        return (self.current_page - 1) * self.per_page_count

    @property
    def end(self):
        return self.current_page * self.per_page_count

    def page_html(self):
        # 如果总页数 <= 11
        if self.max_page_num <= self.max_pager_count:
            pager_start = 1
            pager_end = self.max_page_num
        # 如果总页数 > 11
        else:
            # If the current page <= 5
            if self.current_page <= self.half_max_pager_count:
                pager_start = 1
                pager_end = self.max_pager_count
            else:
                # 当前页 + 5 > 总页码
                if (self.current_page + self.half_max_pager_count) > self.max_page_num:
                    pager_end = self.max_page_num
                    pager_start = self.max_page_num - self.max_pager_count + 1   #倒这数11个
                else:
                    pager_start = self.current_page - self.half_max_pager_count
                    pager_end = self.current_page + self.half_max_pager_count

        page_html_list = []
        # {source:[2,], status:[2], gender:[2],consultant:[1],page:[1]}
        # 首页
        self.params['page'] = 1
        first_page = '首页' % (self.base_url,urlencode(self.params),)
        page_html_list.append(first_page)
        # 上一页
        self.params["page"] = self.current_page - 1
        if self.params["page"] <= 1:
            pervious_page = '上一页' % (self.base_url, urlencode(self.params))
        else:
            pervious_page = '上一页' % ( self.base_url, urlencode(self.params))
        page_html_list.append(pervious_page)
        # 中间页码
        for i in range(pager_start, pager_end + 1):
            self.params['page'] = i
            if i == self.current_page:
                temp = '%s' % (self.base_url,urlencode(self.params), i,)
            else:
                temp = '%s' % (self.base_url,urlencode(self.params), i,)
            page_html_list.append(temp)

        # 下一页
        self.params["page"] = self.current_page + 1
        if self.params["page"] > self.max_page_num:
            self.params["page"] = self.current_page
            next_page = '下一页' % (self.base_url, urlencode(self.params))
        else:
            next_page = '下一页' % (self.base_url, urlencode(self.params))
        page_html_list.append(next_page)

        # 尾页
        self.params['page'] = self.max_page_num
        last_page = '尾页' % (self.base_url, urlencode(self.params),)
        page_html_list.append(last_page)

        return ''.join(page_html_list)


2.2 Routing

       @app.route('/list_t', methods=['GET'])
    def list_t():
        ori_data = ['第一列', '第二列', '空列']
        li = []
        for x in range(1000):
            li.append(ori_data + [x])
        pager_obj = Pagination(request.args.get("page", 1), len(li), request.path, request.args, per_page_count=10)
        index_list = li[pager_obj.start:pager_obj.end]
        pagination_html = pager_obj.page_html()
        return render_template("list_paper.html", index_list=index_list, html=pagination_html)

2.3 html Code

slightly

2.4 demonstration effect

Demonstration effect:

none

Guess you like

Origin www.cnblogs.com/wodeboke-y/p/11610129.html
Tab
Tab
Tab
Tab