Django custom page combined with bootstrap page, djangobootstrap
Django has its own Paginator module. If you want Paginator to provide a list of objects, You can provide methods for objects on each page.
Paginator is not explained here, but a page class is customized to meet the requirements:
Class Pagination (object): "used to select Model Field Values" def _ init _ (self): pass @ classmethod def create_pagination (self, from_name = '', model_name ='', cur_page = 1, start_page_omit_symbol = '... ', end_page_omit_symbol = '... ', one_page_data_size = 10, show_page_item_len = 9): "" paginated Args: from_name: from {from_name} import model_name mode_name: name of the module to be imported from from_name import {model_name} cur_page: what is the ellipsis (Before) used for the number of pages exceeding the limit )... 2 3 4 end_page_omit_symbol: What is the ellipsis (s) used for the excess page number (s) 1 2 3 4... one_page_data_size: the number of show_page_item_len lines displayed on each page: the number of pages that can be clicked. Return: pagination: dict pagination = {'objs': objs, # model data 'all _ obj_counts': all_obj_counts, # Total number of rows of Data 'start _ pos': start_pos, # data starting from data pagination 'end _ pos': end_pos, # 'all _ page': all_page, # the total number of pages of the Data ending with pagination: 'cur _ page': cur_page, # the current page number 'pre _ page': pre_page, # page number of the previous page 'Next _ page': next_page, # page number of the next page 'page _ items ': page_items, page number of pages that can be clicked 'start _ page_omit_symbol': start_page_omit_symbol, # Start ellipsis 'end _ page_omit_symbol': end_page_omit_symbol, # end ellipsis} Raise: none "# exit if not from_name or not model_name: return None import_str = 'from {from_name} import {model_name} 'without entering the required information of the import module }'. format (from_name = from_name, model_name = model_name) # import module exec import_str start_pos = (cur_page-1) * one_page_data_size end_pos = start_pos + one_page_data_size # Find the expected model data find_objs_str = ('{model_name }. objects. all () ''[{start_pos }:{ end_pos}] '. format (model_name = model_name, start_pos = start_pos, end_pos = end_pos) objs = eval (find_objs_str) # calculate the total number of pages find_objs_count_str = '{model_name }. objects. count ()'. format (model_name = model_name) all_obj_counts = eval (partition) all_page = all_obj_counts/one_page_data_size remain_obj = all_obj_counts % one_page_data_size if remain_obj> 0: all_page + = 1 # limit that the current page cannot be less than 1 and greater than the total number of pages cur_page = 1 if cur_page <1 else cur_page = all_page if cur_page> all_page else cur_page # obtain the minimum number of displayed pages page start_page = cur_page-show_page_item_len/2 if start_page> all_page-show_page_item_len: start_page = all_page-Detail + 1 start_page = 1 if start_page <1 else start_page # obtain the maximum page end_page = cur_page + response/2 end_page = all_page if end_page> all_page else end_page if end_page <show_page_item_len and all_page> show_page_item_len: end_page = Response # Get Previous Page pre_page = cur_page-1 pre_page = 1 if pre_page <1 else pre_page # Get next page next_page = cur_page + 1 next_page = all_page if next_page> all_page else next_page character, whether to display if start_page <= 1: start_page_omit_symbol = ''if end_page> = all_page: end_page_omit_symbol ='' # create page_items = range (start_page, end_page + 1) pagination = {'objs': objs, 'all _ obj_counts': all_obj_counts, 'start _ pos': start_pos, 'end _ pos': end_pos, 'all _ page ': all_page, 'cur _ page': cur_page, 'Pre _ page': pre_page, 'next _ page': next_page, 'page _ items ': page_items, 'start _ page_omit_symbol ': start_page_omit_symbol, 'end _ page_omit_symbol ': end_page_omit_symbol,} return pagination
Use bootstrap's css to generate the following Nice-looking html:
<nav aria-label="Page navigation"> <ul class="pagination"> {% if pagination.cur_page != 1 %} <li><a href="?cur_page=1" rel="external nofollow" ><<</a></li> <li> <a href="?cur_page={{ pagination.pre_page }}" rel="external nofollow" aria-label="Previous"> <span aria-hidden="true">«</span> </a> </li> {% endif %} {% for page_item in pagination.page_items %} {% if page_item == pagination.cur_page %} <li><a href="?cur_page={{ page_item }}" rel="external nofollow" rel="external nofollow" >{{ page_item }}</a></li> {% else %} <li><a href="?cur_page={{ page_item }}" rel="external nofollow" rel="external nofollow" >{{ page_item }}</a></li> {% endif %} {% endfor%} {% if pagination.cur_page != pagination.all_page %} <li> <a href="?cur_page={{ pagination.next_page }}" rel="external nofollow" aria-label="Next"> <span aria-hidden="true">»</span> </a> </li> <li><a href="?cur_page={{ pagination.all_page }}" rel="external nofollow" >>></a></li> {% endif %} </ul> </nav>
The view function is as follows:
def blogpage(request): #ojt = BlogPost.object.all() #p = Paginator(ojt,2) #page_count = p.count #page_data = p.page(page) #template = 'blogpage.html' #info = {'page_data':page_data,'page_count':page_count} #return render(request,template,{'page_data':page_data,'page_count':page_count}) try: cur_page = int(request.GET.get('cur_page', '1')) except ValueError: cur_page = 1 pagination = Pagination.create_pagination( from_name='blog.models', model_name='BlogPost', cur_page=cur_page, start_page_omit_symbol = '...', end_page_omit_symbol = '...', one_page_data_size=1, show_page_item_len=5) return render(request, 'blogpage.html',{'pagination':pagination})
As follows:
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.