tornado web frame:
非阻塞服务器,速度快,运用epoll
模板语言+render(),实现根据用户输入,自动渲染页面的动态效果。
在使用模板前需要在setting中设置模板路径:
settings = {
"template_path" : "xxoo";
"static_path“:"ooxx"
"static_usr_prefix":"ox"
}
一、快速实现:
第一步 创建子类
第二布 创建application
第三部 run
二、模板引擎
在html中使用方式有:
{%if/else/for/while/try%} ...{%end%} structrue 结构
{{expression/variable}} 表达式 》》》》需要后端程序中传入变量的值,以键值对的形式: self.render('index.html',ooxx=input_list,aaa=word)
{%module myclass()%} UImodules
{{func()}} UImethods
UImethods / UImodules
定义 >> 导入+注册>> 使用
定义UImethod:
# uimethods.py
def tab(self):
return 'UIMethod'
定义UImodules
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from tornado.web import UIModule
class custom(UIModule):
def render(self, *args, **kwargs):
return 123
导入+注册
import uimodules as md
import uimethods as mt
settings = {
'ui_methods': mt,
'ui_modules': md,
}
使用
<body>
<h1>hello</h1>
{% module custom(123) %}
{{ tab() }}
</body>
内置模板函数 ,重点使用 static_url()
eg: <link href="{{static_url("commons.css")}}" rel="stylesheet" />
效果:自动索引到后台的静态文件,并且生成md5值,返回到客户
<link href="/sss/common.css?v=114b63a591fc6e890f095ddc302666f9" rel="stylesheet" />
模板引擎的原理:
html --> 转换成 字符串函数 “def func():....” ---> compile + exec + 全局变量 --> 执行
详情见 :https://www.cnblogs.com/wupeiqi/p/4592066.html
cookies
self.set_cookie('key','value')
value = self.get_cookie('key')
签名cookie
self.set_secure_cookie("mycookie", "myvalue")
self.get_secure_cookie("mycookie")
settings = {'cookie_secret': 'aiuasdhflashjdfoiuashdfiuh'}