标签:支持 post请求 end 用户 aabb ror bsp 设置 数字
import tornado.web class SatoriHandler(tornado.web.RequestHandler): def get(self, *args, **kwargs): print("get") def initialize(self): print("initialize") def prepare(self): print("prepare") def set_default_headers(self): print("set_default_headers") def write_error(self, status_code, **kwargs): print("write_error") def on_finish(self): print("on_finish")
import tornado.web class SatoriHandler(tornado.web.RequestHandler): def get(self, *args, **kwargs): print("get") self.send_error() def initialize(self): print("initialize") def prepare(self): print("prepare") def set_default_headers(self): print("set_default_headers") def write_error(self, status_code, **kwargs): print("write_error") def on_finish(self): print("on_finish")
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>古明地觉</title> </head> <body> <h1 style="text-align: center">欢迎来到古明地觉的避难小屋</h1> </body> </html>
import tornado.web class SatoriHandler(tornado.web.RequestHandler): def get(self, *args, **kwargs): self.render("satori.html")
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>古明地觉</title> </head> <body> <h1>my name is {{name}}, which comes from {{place}}, people usually call me {{nickname1}}, {{nickname2}}, {{nickname3}} </h1> </body> </html>
import tornado.web class SatoriHandler(tornado.web.RequestHandler): def get(self, *args, **kwargs): # 在html中定义了name,place,nickname1,nickname2,nickname3 # 那么在渲染模板的时候,可以将参数传进去,然后进行替换 # 这些参数可以用关键字参数的形式传进去 # 而且通过这一点也能明白渲染的流程,tornado是先将整个html文件以字符串的形式全部读取进来 # 再按照模板语言进行替换,然后将替换之后的字符串返回给用户浏览器 # 浏览器再解析成页面,这就是模板渲染的流程。 # 因此最终交给浏览器的字符串是不包含大括号的 # 而且定义了几个{{var}},就要传几个,否则会报错name ‘xxx‘ is not defined # 而且写上了{{}},那么{{}}里面必须要传一个变量名,否则会报错,tornado.template.ParseError: Empty expression at xxx self.render("satori.html", name="古明地觉", place="东方地灵殿", nickname1="少女觉", nickname2="觉大人", nickname3="小五")
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>古明地觉</title> </head> <body> <h1>my name is {{info["name"]}}, which comes from {{info["place"]}}, people usually call me {{info["nickname1"]}}, {{info["nickname2"]}}, {{info["nickname3"]}} </h1> </body> </html>
import tornado.web class SatoriHandler(tornado.web.RequestHandler): def get(self, *args, **kwargs): info = {"name": "古明地觉", "place": "东方地灵殿", "nickname1": "少女觉", "nickname2": "觉大人", "nickname3": "小五"} self.render("satori.html", info=info)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>古明地觉</title> </head> <body> <h1> 3 > 2 = {{3 > 2}} <br> bin(10) = {{bin(10)}} <br> 2 << 4 = {{2 << 4}} <br> 1 + 1 = {{1 + 1}} <br> sum(range(100)) = {{sum(range(100))}} <br> len("aaabbbccc") = {{len("aaabbbccc")}} <br> "aaa".upper() = {{"aaa".upper()}} </h1> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>古明地觉</title> </head> <body> {%if name == "古明地盆"%} <h1>我是{{name}},东方地灵殿</h1> {%elif name == "古明地恋"%} <h1>我是{{name}},东方地灵殿</h1> {%else%} <h1>管你是谁,我喜欢古明地两姐妹</h1> {%end%} </body> </html>
import tornado.web class SatoriHandler(tornado.web.RequestHandler): def get(self, *args, **kwargs): name = "古明地盆" self.render("satori.html", name=name)
import tornado.web class SatoriHandler(tornado.web.RequestHandler): def get(self, *args, **kwargs): name = "dadasdasdasdas" self.render("satori.html", name=name)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>古明地觉</title> </head> <body> {%for v in info%} <h1>{{v}}: {{info[v]}}</h1> {%end%} </body> </html>
import tornado.web class SatoriHandler(tornado.web.RequestHandler): def get(self, *args, **kwargs): info = {"name": "古明地盆", "gender": "女", "from": "东方地灵殿"} self.render("satori.html", info=info)
标签:支持 post请求 end 用户 aabb ror bsp 设置 数字
原文地址:https://www.cnblogs.com/traditional/p/9571752.html