tornado 模版
2024-08-30 19:01:55
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}} 密码:{{psd}} <br> {% for i in range(1,9) %} <!--百分号表示执行python语句-->
{% set i = i*3 + 1 %}
i = {{i}}
{% end %} <!--这里必须有 end -->
<br> {% set a = 1 %}
{% while a < 5 %}
{% set a += a*2 %}
a = {{a}}
{% end %} <!--这里必须有 end -->
<br> {% set b = 1 %}
{% if b < 5 %}
{% set b += 1 %}
b = {{b}}
{% 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>
最新文章
- iOS关于启动页自定义特殊处理
- ASP.Net MVC开发基础学习笔记(2):HtmlHelper与扩展方法
- Linux 进程间通信(一)
- 微信小程序全面实战,架构设计 &;&; 躲坑攻略(小程序入门捷径教程)
- WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResourceMapping
- Codeforces Round #244 (Div. 2) B. Prison Transfer 线段树rmq
- jquery 下拉多选插件
- SRM 624 Building Heights DivI 解读
- 字符、字符集、编码,以及它们python中会遇到的一些问题(下)
- 使用CEF的JSON解析功能
- Android设计开发笔记
- Hive之SerDe&;Beeline
- pyinstaller深入使用,打包指定模块,打包静态文件
- linux学习第十七天 (Linux就该这么学)
- spring-boot项目的新建(出生)
- 校园电商项目3(基于SSM)——配置Maven
- javascript:void(0) 和 href=";#";的区别
- 为什么MySQL不推荐使用子查询和join
- 蓝桥杯 历届试题 约数倍数选卡片 (经典数论+DFS)
- iOS开发--UILabel根据内容自动调整高度
热门文章
- [COGS2652]秘术「天文密葬法」
- [NOIP2016 TG D2T3]愤怒的小鸟
- Retrofit工具类
- bzoj 1150&;2151&;2288(双向链表+堆)(贪心)
- bzoj 2095 [Poi2010]Bridges 判断欧拉维护,最大流+二分
- HDU1260DP
- 基于tcp交互的python聊天程序
- 确保web安全的https、确认访问用户身份的认证(第七章、第八章)
- bzoj 1036: [ZJOI2008]树的统计Count——树链剖分
- 【BZOJ】4558: [JLoi2016]方