链接:http://demo.pythoner.com/itt2zh/ch2.html
来到了第二章;模板与表单
1 # -*- coding:utf-8 -*- 2 3 __author__ = ‘roy‘ 4 5 import os.path 6 7 import tornado.httpserver 8 import tornado.ioloop 9 import tornado.options 10 import tornado.web 11 12 from tornado.options import define, options 13 14 define("port", default=8000, help="run on the given port", type=int) 15 16 17 class IndexHandler(tornado.web.RequestHandler): 18 def get(self): 19 self.render(‘index.html‘) 20 21 22 class PoemPageHandler(tornado.web.RequestHandler): 23 def post(self): 24 noun1 = self.get_argument(‘noun1‘) 25 noun2 = self.get_argument(‘noun2‘) 26 verb = self.get_argument(‘verb‘) 27 noun3 = self.get_argument(‘noun3‘) 28 self.render(‘poem.html‘, roads=noun1, wood=noun2, made=verb, 29 difference=noun3) 30 31 32 if __name__ == ‘__main__‘: 33 tornado.options.parse_command_line() 34 app = tornado.web.Application( 35 handlers=[(r‘/‘, IndexHandler), (r‘/poem‘, PoemPageHandler)], 36 # 模板的路径 37 template_path=os.path.join(os.path.dirname(__file__), "templates") 38 ) 39 # 服务器 40 http_server = tornado.httpserver.HTTPServer(app) 41 http_server.listen(options.port) 42 tornado.ioloop.IOLoop.instance().start()
(1)os.path
template_path=os.path.join(os.path.dirname(__file__), "templates"):获取templates的路径
os.path.dirname(__file__):输出本文件的完整路径
os.path.join:链接两个文件名地址
(2)render:渲染模板
1 <!DOCTYPE html> 2 <html> 3 <head><title>Poem Maker Pro</title></head> 4 <body> 5 <h1>Enter terms below.</h1> 6 <form method="post" action="/poem"> 7 <p>Plural noun<br><input type="text" name="noun1"></p> 8 <p>Singular noun<br><input type="text" name="noun2"></p> 9 <p>Verb (past tense)<br><input type="text" name="verb"></p> 10 <p>Noun<br><input type="text" name="noun3"></p> 11 <input type="submit"> 12 </form> 13 </body> 14 </html>
1 <!DOCTYPE html> 2 <html> 3 <head><title>Poem Maker Pro</title></head> 4 <body> 5 <h1>Your poem</h1> 6 <p>Two {{roads}} diverged in a {{wood}}, and I—<br> 7 I took the one less travelled by,<br> 8 And that has {{made}} all the {{difference}}.</p> 9 </body> 10 </html>
(3)tornado的变量里面可以带表达式;标签基本是一样的
(4)static_url 生成静态的 URL
1 # -*- coding:utf-8 -*- 2 3 import os.path 4 import random 5 6 import tornado.httpserver 7 import tornado.ioloop 8 import tornado.options 9 import tornado.web 10 11 from tornado.options import define, options 12 13 define("port", default=8000, help="run on the given port", type=int) 14 15 16 class IndexHandler(tornado.web.RequestHandler): 17 def get(self): 18 self.render(‘index.html‘) 19 20 21 class MungedPageHandler(tornado.web.RequestHandler): 22 def map_by_first_letter(self, text): 23 mapped = dict() 24 for line in text.split(‘\r\n‘): 25 for word in [x for x in line.split(‘ ‘) if len(x) > 0]: 26 if word[0] not in mapped: mapped[word[0]] = [] 27 mapped[word[0]].append(word) 28 return mapped 29 30 def post(self): 31 source_text = self.get_argument(‘source‘) 32 text_to_change = self.get_argument(‘change‘) 33 source_map = self.map_by_first_letter(source_text) 34 change_lines = text_to_change.split(‘\r\n‘) 35 self.render(‘munged.html‘, source_map=source_map, change_lines=change_lines, 36 choice=random.choice) 37 38 39 if __name__ == ‘__main__‘: 40 tornado.options.parse_command_line() 41 app = tornado.web.Application( 42 handlers=[(r‘/‘, IndexHandler), (r‘/poem‘, MungedPageHandler)], 43 template_path=os.path.join(os.path.dirname(__file__), "templates"), 44 # 静态文件的路径 45 static_path=os.path.join(os.path.dirname(__file__), "static"), 46 # debug 开启 47 debug=True 48 ) 49 http_server = tornado.httpserver.HTTPServer(app) 50 http_server.listen(options.port) 51 tornado.ioloop.IOLoop.instance().start()
时间: 2025-01-08 16:39:22