tornado 模版继承 函数和类的调用

时间:2023-03-10 03:05:30
tornado 模版继承 函数和类的调用

模版继承、函数和类的调用

目录结构

tornado 模版继承 函数和类的调用

lesson5.py

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

 import tornado.web
import tornado.httpserver
import tornado.options
import tornado.ioloop
import util.ui_modules
import util.ui_methods 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):
self.render('extend04.html',
haha = self.haha,
) def haha(self):
return '哈哈哈哈' class NotFoundHandler(tornado.web.RequestHandler): # 若路由未匹配上,则跳转到出错页面 def get(self, *args, **kwargs): # 这里要发送404 否则会出现 405
self.send_error(404) def write_error(self, status_code, **kwargs):# 重写错误页面
self.render('error.html') #会覆盖前面的 self.render('error.html') application = tornado.web.Application(
handlers = [
(r"/",MainHandler),
(r"/(.*)", NotFoundHandler),
],
template_path = 'template', # 指定路径
static_path= 'static',
ui_methods=util.ui_methods, # 等号左侧是固定写法,不能变
ui_modules=util.ui_modules, # 等号左侧是固定写法,不能变
debug = True # 调试模式 文件修改后 自动重启服务器
) if __name__ == '__main__':
print(options.port)
print(options.version)
tornado.options.parse_command_line()
# app = tornado.web.Application( handlers=[(r'/',MainHandler),] )
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(options.port) # 监听端口
tornado.ioloop.IOLoop.instance().start() #服务器自动循环 等待访问

extend04.html

 {% extends base03.html %}<!--extends 继承base03.html,一般只继承一个 -->
{% block title %} base_300 {% end %}<!--block 修改block里的内容 --> {% block body %}<!--block 修改block里的内容 -->
hello everyone !
{% include include05.html %} <!--include 包含(不是修改)include05.html 被导入的文件不能含extend 、block-->
<br>
{% import time %} <!--引入模块 -->
{{ time.time() }}
<br>
{{ haha() }}<!--调用函数 -->
<br>
{% from util.mod_file import adda,upper %}<!--调用自定义模块里的函数 -->
{{ adda(3,5) }}
<br>
{% module UiModule() %}<!--调用自定义模块里的类 -->
<br>
{{methods1()}}<!--调用自定义模块里的函数 -->
<br>
{% apply upper %} <!--使用函数的作用范围到最近的{%end%}为止 -->
lasdjflkdsjfl
{% end %}
<br>
{% raw linkify('百度raw版:http://www.baidu.com') %}<!--html解析 a标签 -->
<br>
{{ linkify('百度:http://www.baidu.com') }}<!--html不解析 a标签 -->
<br>
{{ adda(3,5) }}
<br>
{% module Advertisement() %} <!--案例 植入广告 --> {% end %}

base03.html

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %} base03 {% end %}</title>
<link rel="shortcut icon" href="{{ static_url('images/favicon.ico') }}">
<style>
div{
margin: 30px
}
</style>
</head>
<body>
<div>
{% block body %}
hello world !
{% end %}
</div>
</body>
</html>

include05.html

 <br>
this is tornado include
<br>

mod_file.py

 def adda(a,b):
return '乘积是:' + str(a*b) def upper(a):
return a.upper()

ui_methods.py

 '''

 this is ui_method
''' def methods1(self):
return 'ui_methods1'

ui_modules.py

 '''
this is ui_modules '''
from tornado.web import UIModule # 必须导入这个 UIModule 类 class UiModule(UIModule):# 固定继承类 UIModule def render(self,*args,**kargs): #固定写法 render(self,*args,**kargs)
return '我是ui_modules' ''' 案例:植入广告 '''
class Advertisement(UIModule): def render(self, *args, **kwargs):
return self.render_string('06ad.html') def css_files(self):
return "/static/css/King_Chance_Layer7.css" def javascript_files(self):
return ["/static/js/jquery_1_7.js",
"/static/js/King_Chance_Layer.js",
"/static/js/King_layer_test.js",
]