tornado 模版

时间:2023-03-10 03:05:30
tornado 模版

tornado 模版语法

取消转义 :

  取消项目转义 :autoescape = None

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

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

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

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

  lesson4.py

 # -*- coding:utf-8 -*-

 import tornado.web
import tornado.httpserver
import tornado.options
import tornado.ioloop
import time from tornado.options import define,options define('port', default=8080, help='run port', type=int)
define('version', default='0.0.1', help='version 0.0.1', type=str) class MainHandler(tornado.web.RequestHandler): def get(self, *args, **kwargs):
self.render('template.html') def post(self, *args, **kwargs):
user = self.get_argument('user','none')
psd = self.get_argument('password','none')
bd = '<a href="https://www.baidu.com" target="_blank">百度</a>'
self.render('template02.html',
user = user,
psd = psd,
time = time,
bd = bd,
) class NotFoundHandler(tornado.web.RequestHandler): def get(self, *args, **kwargs):
self.send_error(404) def write_error(self, status_code, **kwargs):
self.render('error.html') application = tornado.web.Application(
handlers = [
(r"/",MainHandler),
(r"/post",MainHandler),
(r"/(.*)", NotFoundHandler),
],
template_path = 'template',
static_path='static',
# autoescape = None, # 整个项目不转义 尽量不要用
debug = True
) if __name__ == '__main__':
print(options.port)
print(options.version)
tornado.options.parse_command_line()
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()

template.html

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>template_lesson4</title>
<style>
div{
margin: 30px
}
</style>
</head>
<body>
<div>
hello world !
<form method="post" action="/post" target="_blank"> <p>用户名<br><input type="text" name="user"></p> <p>密码<br><input type="password" name="password"></p> <input type="submit"> </form>
</div>
</body>
</html>

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

 <!DOCTYPE html>
{# autoescape None #} <!--仅在此模版去除转义 尽量不要用-->
<html lang="en">
<head>
<meta charset="UTF-8">
<title>template02_lesson4</title>
<style>
div{
margin: 30px
}
a{
text-decoration:none;
}
</style>
</head>
<body>
<div>
登录成功 ! <br>
用户名:{{user}} &nbsp;&nbsp; 密码:{{psd}} <br> {% for i in range(1,9) %} <!--百分号表示执行python语句-->
{% set i = i*3 + 1 %}
i = {{i}} &nbsp;
{% end %} <!--这里必须有 end -->
<br> {% set a = 1 %}
{% while a < 5 %}
{% set a += a*2 %}
a = {{a}} &nbsp;
{% end %} <!--这里必须有 end -->
<br> {% set b = 1 %}
{% if b < 5 %}
{% set b += 1 %}
b = {{b}} &nbsp;
{% end %} <!--这里必须有 end -->
<br> 时间戳1 :{{ time.time() }}
<br> 时间戳2 :{# time.time() #} <!--井号表示注释掉了不执行 不显示-->
<br>
时间戳3 :{{! time.time() }} <!--感叹号表示不转义 感叹号前面无空格-->
<br>
百度链接1: <a href="https://www.baidu.com" target="_blank">百度</a> <br> 百度链接2: {{bd}} <br> <!--花括号里的链接不会被Html解析--> 百度链接3: {% raw bd %} <br> <!--raw 此行取消转义, html 会解析--> 百度链接4: {{escape(bd)}} <br> <!--escape 此行强制转义, 不会被Html解析-->
</div>
<div>
{% set i = 0 %}
{% if i < 1 %}
<img src="static/imgs/01.jpg" width="300px" height="200px"> <!--静态文件的两种添加方式-->
<img src="{{ static_url('imgs/02.jpg') }}" width="300px" height="200px"> <!--静态文件的两种添加方式-->
{% end %}
<br>
</div> </body>
</html>