Python custom page-out program

Source: Internet
Author: User

To prevent XSS as a cross-site scripting attack, you need to add safe
# route from Django.conf.urls import urlfrom django.contrib import adminfrom mypaginator import viewsurlpatterns = [url (r ') ^admin/', admin.site.urls), url (r ' ^hosts.html$ ', views.hosts),]# view function def hosts (request): "" "Create Test Data" "" # for I In Range (303): # NAME = "c%s.com"% i # port = i # models. Host.objects.create (hostname=name,port=port) # return HttpResponse ("OK") # Default shows 10 data per page items_per_page = 10 # ago url:http://127.0.0.1:8001/hosts.html?page=2 current_page = Int (request.    Get.get (' page ')) # X-items_per_page* (x-1) ~ items_per_page*x Start_item = items_per_page* (current_page-1) End_item = items_per_page*current_page # Gets the corresponding number of items Host_list = models from the database in the form of a slice. Host.objects.all () [Start_item:end_item] page_str = "" <a href= "hosts.html?page=1" >1</a> < A href= "hosts.html?page=2" >2</a> <a href= "hosts.html?page=3" >3</a> "" "Return Render (req Uest, ' hosts.html ', localS ()) # Front end html...<div class= "pagination" > {{Page_str|safe}}</div> .... 

  

Customizing the paging component
# route from Django.conf.urls import urlfrom django.contrib import adminfrom mypaginator Import viewsurlpatterns = [    URL (r ' ^admin/', admin.site.urls),    url (r ' ^hosts.html$ ', views.hosts),]

  

# modelsfrom django.db Import models# Create your models Here.class Host (models. Model):    hostname = models. Charfield (max_length=32)    port = models. Charfield (max_length=10)

  

# Pagination component:.. \paginator\utils\paginator.py#!/usr/bin/python#-*-coding:utf-8-*-class paginator (object): Def __init__ (Self,all_        count,current_page,base_url,per_page=10,per_page_count=11): "" "Paging component initialization:p Aram All_count: Total number of pieces of data  :p Aram Current_page: Current page:p aram base_url: Base URL:p Aram Per_page: Number of entries per page:p Aram Per_page_count: Number of pages displayed "" "Self.all_count = all_count self.current_page = current_page Self.base_url = Base_u RL Self.per_page = Per_page Self.per_page_count = Per_page_count # Calculates the true number of pages Pager_count, REM Ainder = Divmod (Self.all_count, self.per_page) if 0! = remainder:pager_count + 1 Self.pager_cou NT = pager_count # displays 11 page numbers each time (except for previous, next, first, and last) and lets the current selection page always be centered self.half_per_page_count = Int (self.per_page_c OUNT/2) @property def start (self): Start index of "" "Data entry # X-items_per_page* (x-1) ~ Items_per_ Page*x:reTurn: "" "Return Self.per_page * (self.current_page-1) @property def end (self): number of" ""  End of Entry index # items_per_page* (x-1) ~ Items_per_page*x:return: "" "Return Self.per_page        * Self.current_page @property def page_html (self): # get the correct start and end page numbers # to determine if the true number of pages is more than Per_page_count If Self.pager_count > Self.per_page_count: # If current page < Half_per_page_count if self.current_p            Age <= Self.half_per_page_count:pager_start = 1 Pager_end = Self.per_page_count # If the current page number is greater than half_per_page_count and is less than Pager_count-half_per_page_count elif self.current_page <= SELF.PA                Ger_count-self.half_per_page_count:pager_start = Self.current_page-self.half_per_page_count            Pager_end = self.current_page + self.half_per_page_count # If the current page number is greater than pager_count-half_per_page_count            Else    Pager_start = self.pager_count-self.per_page_count + 1 pager_end = Self.pager_count Else: Pager_start = 1 Pager_end = Self.pager_count page_list = [] first_page = ' <a href= ' {}?p            Age=1 "> Home </a>". Format (Self.base_url) page_list.append (first_page) if self.current_page > 1:            Prev = ' <a href= ' {}?page={} ' > previous page </a> '. Format (self.base_url,self.current_page-1) Else: Prev = ' <a href= ' javascript:void (0) "disabled=" true "> Previous </a> ' Page_list.append (prev) # cycle Generation HT ML for I in range (Pager_start, Pager_end + 1): if i = = Self.current_page:tpl = ' <a cl Ass= "Active" href= "{}?page={}" >{}</a> ". Format (self.base_url,i, i) Else:tpl = ' <a hr Ef= "{}?page={}" >{}</a> ". Format (self.base_url,i, i) page_list.append (TPL) if Self.current_page & Lt   Self.pager_count:         NEX = ' <a href= ' {}?page={} ' > Next </a> '. Format (self.base_url,self.current_page + 1) Else: NEX = ' <a href= ' javascript:void (0) "disabled=" true "> Next </a>". Format (self.current_page + 1) page_lis T.append (NEX) last_page = ' <a href= ' {}?page={} ' > Last </a> '. Format (Self.base_url,self.pager_count) p Age_list.append (last_page) page_str = "". Join (page_list) return page_str

  

# view function from django.shortcuts import render,httpresponse# Create your view here.from mypaginator import Modelsfrom UTILS.PA     Ginator Import Paginatordef hosts (Request): "" "Create Test Data" "" # For I in range (303): # NAME = "c%s.com"% i # Port = i # models.        Host.objects.create (hostname=name,port=port) # return HttpResponse ("OK") # back end generates good page HTML and then returns to the front display _PAGE_STR = "" " <a href= "hosts.html?page=1" >1</a> <a href= "hosts.html?page=2" >2</a> <a HRE f= "hosts.html?page=3" >3</a> "" "# Front-end request url:http://127.0.0.1:8001/hosts.html?page=2 current_page = Int (re Quest. Get.get (' page ')) # First gets the total number of data bars All_count = models. Host.objects.all (). Count () pager = Paginator (all_count=all_count,current_page=current_page,base_url=request.path_ Info) # Items Host_list = models that have the corresponding number of pages from the database in the form of slices. Host.objects.all () [pager.start:pager.end] Page_str = pager.page_html return render (Request, ' hosts.html ', locals ())

# front html<!    DOCTYPE html>

  

Python custom page-out program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.