The tornado framework of Python implements the paging function

Source: Internet
Author: User



1. As a result, the HTML page and the request processing of the website are placed in different places.









start.py Code


import tornado.ioloop
import tornado.web
from controllers import home
Settings = {
'static path': 'statics', static file configuration, special handling required
"Static ﹣ URL ﹣ prefix ':' / SSS / ', the name of the beginning of the tag file"
}
#Route mapping, corresponding to different classes according to different URLs
application = tornado.web.Application([
(r"/index/(?P<page>\d*)", home.IndexHandler),
], * * settings) ා based on regular routing,? P < Page > indicates a name for the regular matching content
if __name__==‘__main__‘:
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()


Home.py inside the Code


import tornado.web
List_Info=[
{‘username‘:‘jay‘,‘email‘:‘[email protected]‘}
]
class IndexHandler(tornado.web.RequestHandler):
def get(self,page):
#Five pieces of data per page
#Page is the current page
#Page 1: 0:5 list info [0:5]
#Page 2: 5:10
#start: (page-1)*5
#end:   (page)*5
try:page=int(page)
except:page=1
Print (page)
If page <1:
Page =1
start = (page - 1)*5
end = page * 5
current_ist = List_Info[start:end]
Self. Render ('e: \ \ exercise 2 \ \ tornado \ \ Day2 \ \ views \ \ home \ \ index. HTML ', list = current_ist, page = page)
#E: \ \ exercise 2 \ \ tornado \ \ Day2 \ \ views \ \ home \ \ index.html
def post(self,page):
user = self.get_argument(‘username‘)
email = self.get_argument(‘email‘)
temp = {‘username‘:user,‘email‘:email}
List_Info.append(temp)
self.redirect(‘/index/‘+page)


The index.html code inside the home file


<! DOCTYPE html>


Run results


Finally, we modify the code inside the home to enable the selection of the page function


5] page 2: 5:10# = 1 List page = [] print (all page) if p U p age): if P + 1 = = page: temp = '< a class =' Act ive ' href= '/index/%s ' >%s&amp;  Lt;/a> '% (p + 1, p + 1) else:temp = ' <a href= '/index/%s ' >%s</a> '% (p + 1, p + 1) list_page.append (temp) Else:for p in range (PAGE-5,PAGE+4): if                P+1 = = Page:temp = ' <a class= ' active ' href= '/index/%s ' >%s</a> '% (p+1,p+1) Else:temp = ' <a href= '/index/%s ' >%s</a> '% (p + 1, p + 1) list_pa ge.append ( Temp) Str_page = '. Join (list_page) # str_page = List_page, without writing a join will display a comma on the page self.render (' e:\\ exercise 2\\tor Nado_day2\\views\\home\\index.html ', list= current_ist,page=page,str_page=str_page) #E: \ \ Practice 2\\tornado_day2\\views \\home\\index.html def post (self, page): User = Self.get_a rgument (' username ') email = self.get_argument (' email ') temp = {' username ': u Ser, ' email ': email} list_info.append (temp) self.redirect ('/index/' +page) print (list_info)

Make less changes to index.html files


Finally, the code inside the home is encapsulated,


One page per five data, just how many pag es self.all_page=all_page if c > 0: # there The remainder is one more page All_page + = 1 Self.all_page = all_page# Gets the maximum page try:current_page=int (current_pag e) except:current_page=1 if Current_page <1:current_page =1 self.current_p        Age = Current_page @property def strat (self): return (self.current_page-1) @property def end ( self): Return self.current_page * 5 def get_page (self,base_url): self.all_page, C = Divmod (Len (list_info), 5) # One page per five data, judging how many pages if C > 0: # More than one page Self.all_page + = 1 List_page = [] If SELF.CURRENT_PA GE < 5:FOR p in range (9): if p + 1 = = Self.current_page:temp = ' <a class= "active" href= "%s%s ">%s </a>"% (base_url,p + 1, p + 1) else:temp = ' <a href= "%s%s" >%s</a > '% (base_url,p + 1, p + 1) list_page.append (temp) elif self.current_page > Self.all_page-5 : # Determine whether the page is in the top or last five page for P in range (self.all_page-9, self.all_page): if p = = Self.current_page                    : temp = ' <a class= ' active  ' href= '%s '%s ' >%s</a> '% (Base_url, p, p) Else:        temp = ' <a href= '%s%s ' >%s</a> '% (Base_url, p, p) list_page.append (temp) Else:for p in range (self.current_page-5, Self.current_page + 4): if p = = self.current_p Age:temp = ' <a class= ' active ' href= '%s%s ' >%s</a> '% (Base_url, p, p) Else : TEMp = ' <a href= '%s%s ' >%s</a> '% ( = Li Page obj. Current page says to pass the processed pages into t he Def post (self, page): User = Self.get_argument (' username ') email = self.get_argument (' email ') temp = {' username ': User, ' email ': email} list_info.append (temp) Self.redir ECT ('/index/' +page) print (list_info)

Run


The tornado framework of Python implements the paging function




Related Article

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.