tornado 模版

tornado 模版语法

取消转义 :

  取消项目转义 :autoescape = None

  取消模版转义:{% autoescape None %}

  取消行转义   :{% raw bd %}

强制转义(行)   :{{escape(bd)}}

循环语句              :if while for
静态文件    :static

  lesson4.py

 1 # -*- coding:utf-8 -*-
 2 
 3 import tornado.web
 4 import tornado.httpserver
 5 import tornado.options
 6 import tornado.ioloop
 7 import time
 8 
 9 from tornado.options import define,options
10 
11 define('port', default=8080, help='run port', type=int)
12 define('version', default='0.0.1', help='version 0.0.1', type=str)
13 
14 class MainHandler(tornado.web.RequestHandler):
15 
16     def get(self, *args, **kwargs):
17         self.render('template.html')
18 
19     def post(self, *args, **kwargs):
20         user = self.get_argument('user','none')
21         psd = self.get_argument('password','none')
22         bd = '<a href="https://www.baidu.com" target="_blank">百度</a>'
23         self.render('template02.html',
24                     user = user,
25                     psd = psd,
26                     time = time,
27                     bd = bd,
28                    )
29 
30 
31 class NotFoundHandler(tornado.web.RequestHandler):
32 
33     def get(self, *args, **kwargs):
34         self.send_error(404)
35 
36     def write_error(self, status_code, **kwargs):
37         self.render('error.html')
38 
39 
40 application = tornado.web.Application(
41     handlers = [
42     (r"/",MainHandler),
43     (r"/post",MainHandler),
44     (r"/(.*)", NotFoundHandler),
45     ],
46     template_path = 'template',
47     static_path='static',
48     # autoescape = None, # 整个项目不转义 尽量不要用
49     debug = True
50 )
51 
52 
53 if __name__ == '__main__':
54     print(options.port)
55     print(options.version)
56     tornado.options.parse_command_line()
57     http_server = tornado.httpserver.HTTPServer(application)
58     http_server.listen(options.port)
59     tornado.ioloop.IOLoop.instance().start()

template.html

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>template_lesson4</title>
 6     <style>
 7         div{
 8             margin: 30px
 9         }
10     </style>
11 </head>
12 <body>
13     <div>
14         hello world !
15         <form method="post" action="/post" target="_blank">
16 
17             <p>用户名<br><input type="text" name="user"></p>
18 
19             <p>密码<br><input type="password" name="password"></p>
20 
21             <input type="submit">
22 
23         </form>
24     </div>
25 </body>
26 </html>

  template2.html   ----> 本文重点

 1 <!DOCTYPE html>
 2 {# autoescape None #} <!--仅在此模版去除转义 尽量不要用-->
 3 <html lang="en">
 4 <head>
 5     <meta charset="UTF-8">
 6     <title>template02_lesson4</title>
 7     <style>
 8         div{
 9             margin: 30px
10         }
11         a{
12             text-decoration:none;
13         }
14     </style>
15 </head>
16 <body>
17     <div>
18         登录成功 ! <br>
19         用户名:{{user}}   &nbsp;&nbsp;   密码:{{psd}} <br>
20 
21         {% for i in range(1,9) %}  <!--百分号表示执行python语句-->
22             {% set i = i*3 + 1 %}
23             i = {{i}} &nbsp;
24         {% end %} <!--这里必须有 end -->
25         <br>
26 
27         {% set a = 1 %}
28         {% while a < 5 %}
29             {% set a += a*2 %}
30             a = {{a}} &nbsp;
31         {% end %}  <!--这里必须有 end -->
32         <br>
33 
34         {% set b = 1 %}
35         {% if b < 5 %}
36             {% set b += 1 %}
37             b = {{b}} &nbsp;
38         {% end %}  <!--这里必须有 end -->
39         <br>
40 
41         时间戳1 :{{ time.time() }}
42         <br>
43 
44         时间戳2 :{# time.time() #}  <!--井号表示注释掉了不执行 不显示-->
45         <br>
46         时间戳3 :{{! time.time() }}  <!--感叹号表示不转义 感叹号前面无空格-->
47         <br>
48         百度链接1: <a href="https://www.baidu.com" target="_blank">百度</a> <br>
49 
50         百度链接2: {{bd}} <br>  <!--花括号里的链接不会被Html解析-->
51 
52         百度链接3: {% raw bd %} <br>  <!--raw 此行取消转义, html 会解析-->
53 
54         百度链接4: {{escape(bd)}}  <br>  <!--escape 此行强制转义, 不会被Html解析-->
55     </div>
56     <div>  
57         {% set i = 0 %}
58         {% if i < 1 %}
59             <img src="static/imgs/01.jpg" width="300px" height="200px">    <!--静态文件的两种添加方式-->
60             <img src="{{ static_url('imgs/02.jpg') }}" width="300px" height="200px">   <!--静态文件的两种添加方式-->
61         {% end %}
62         <br>
63     </div>
64 
65 </body>
66 </html>
原文地址:https://www.cnblogs.com/zlsgh/p/8478600.html