Pager
<!--前端--> {% for book in page_queryset %} <p>{{ book.title }}</p> {% endfor %} {{ page_obj.page_html|safe }}
# 后端 book_list = models.Book.objects.all() current_page = request.GET.get("page",1) all_count = book_list.count() page_obj = Pagination(current_page=current_page,all_count=all_count,per_page_num=10,pager_count=5) page_queryset = book_list[page_obj.start:page_obj.end]
Pager template code
class Pagination (Object): DEF the __init__ (Self, current_page, ALL_COUNT are, per_page_num = 2, pager_count =. 11 ): "" " Package tab data : param current_page: this page : param all_count: Total number of stripes in the database : param the number of data per page is displayed: per_page_num : param pager_count: up to displayed page number usage: QuerySet = model.objects.all () page_obj = Pagination (current_page, ALL_COUNT are) page_data = QuerySet [page_obj.start: page_obj.end] acquiring data instead of using the original page_data queryset retrieve a front tab style with page_obj.page_html "" " the try : current_page = int(current_page) 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_num = per_page_num # 总页码 all_pager, tmp = divmod(all_count, per_page_num) if tmp: all_pager += 1 self.all_pager = all_pager self.pager_count = pager_count self.pager_count_half = int ((pager_count -. 1) / 2 ) @Property DEF Start (Self): return (self.current_page -. 1) * self.per_page_num @Property DEF End (Self): return self.current_page * self.per_page_num DEF page_html (Self): # if the total p <11: IF self.all_pager <= self.pager_count: pager_start =. 1 pager_end = self.all_pager. 1 + # total p>. 11 the else : # up on this page if <= page display a page number 11/2 if self.current_page <= self.pager_count_half: pager_start = 1 pager_end = self.pager_count + 1 # 当前页大于5 else: # 页码翻到最后 if (self.current_page + self.pager_count_half) > self.all_pager: pager_end = self.all_pager + 1 pager_start = self.all_pager - self.pager_count + 1 else: pager_start = self.current_page - self.pager_count_half pager_end = self.current_page + self.pager_count_half + 1 page_html_list = [] # 添加前面的nav和ul标签 page_html_list.append(''' <nav aria-label='Page navigation>' <ul class='pagination'> ''') first_page = '<li><a href="?page=%s">首页</a></li>' % (1) page_html_list.append(first_page) if self.current_page <= 1: prev_page = '<li class="disabled"><a href="# "> Previous </a> </ Li> prev_pagethe else' := '<li><a href="?page=%s">上一页</a></li>' % (self.current_page - 1,) page_html_list.append(prev_page) for i in range(pager_start, pager_end): if i == self.current_page: temp = '<li class="active"><a href="?page=%s">%s</a></li>' % (i, i,) else: temp = '<li><a href="?page=%s">%s</a></li>' % (i, i,) page_html_list.append(temp) if self.current_page >= self.all_pager: next_page = '<li class="disabled"><a href="#">下一页</a></li>' else: next_page = '<li><a href="?page=%s">下一页</a></li>' % (self.current_page + 1,) page_html_list.append(next_page) last_page = '<li><a href="?page=%s">尾页</a></li>' % (self.all_pager,) page_html_list.append(last_page) # 尾部添加标签 page_html_list.append(''' </nav> </ul> ''') return ''.join(page_html_list)
from app01.utils.mypage import Pagination def book(request): book_list = models.Book.objects.all() current_page = request.GET.get("page",1) all_count = book_list.count() page_obj = Pagination(current_page=current_page,all_count=all_count,per_page_num=10) page_queryset = book_list[page_obj.start:page_obj.end] return render(request,'booklist.html',locals())
<body> <div class="container"> <div class="row"> <div class="col-md-8 col-md-offset-2"> {% for book in page_queryset %} <p>{{ book.title }}</p> {% endfor %} {{ page_obj.page_html|safe }} </div> </div> </div> </body>
Django serialization component
When to Use
Backend want to directly instantiate objects out of the data is sent directly to the client, so this time, you can use serialization to provide us Django
# Rear: DEF Ser (Request): # get all user objects in the user table inside user_list = models.User.objects.all () # Import module built sequence from django.core Import serializers # invoked the module method, the first parameter is in what would you like the way your serialized data RET = serializers.serialize ( ' JSON ' , user_list) return the render (Request, ' index.html ' , about locals ()) # front end to give : [{ " Model " : " app01.user " , " PK ": 1,"fields": {"username": "jason","age": 18,"gender": 1} }......]
bulk_create bulk insert data (high)
= L [] for I in Range (10000 ): l.append (models.Book2 (name = ' % s of the book ' % I)) models.Book2.objects.bulk_create (L) # Bulk insert data
field parameter choices
Why
1. For the type of sex, false negative, marital status field has a fixed field data field parameter choices may be
2 to save space, because the stored digital
how to use
# The models.py file: class the User (models.Model): username = models.CharField (MAX_LENGTH = 32 ) Age = models.IntegerField () choices = ( ( . 1, ' M ' ), (2, ' F ' ) , (3, ' other ' ) ) Gender = models.IntegerField (choices = choices) # specify the parameter choices "" " 1 stored inside the choice list of Chinese digital correspondence relationship print (user_obj.get_gender_display ()) as long as the choices field in obtaining the number corresponding annotation fixed grammar get_ field names _display () 2 deposit does not set out the figures do not show an error or a digital "" "
# The views.py file DEF UserList (Request): USER_OBJ = models.User.objects.filter (= PK. 1 ) .first () Gender = user_obj.get_gender_display () # obtain a corresponding annotation gender, such as 'M'