模版继承、函数和类的调用
目录结构
lesson5.py
1 # -*- coding:utf-8 -*-
2
3 import tornado.web
4 import tornado.httpserver
5 import tornado.options
6 import tornado.ioloop
7 import util.ui_modules
8 import util.ui_methods
9
10 from tornado.options import define,options
11
12 define('port', default=8080, help='run port', type=int) # 定义端口
13 define('version', default='0.0.1', help='version 0.0.1', type=str) # 定义版本
14
15
16 class MainHandler(tornado.web.RequestHandler): # 定义路由
17
18 def get(self):
19 self.render('extend04.html',
20 haha = self.haha,
21 )
22
23 def haha(self):
24 return '哈哈哈哈'
25
26
27 class NotFoundHandler(tornado.web.RequestHandler): # 若路由未匹配上,则跳转到出错页面
28
29 def get(self, *args, **kwargs): # 这里要发送404 否则会出现 405
30 self.send_error(404)
31
32 def write_error(self, status_code, **kwargs):# 重写错误页面
33 self.render('error.html') #会覆盖前面的 self.render('error.html')
34
35
36 application = tornado.web.Application(
37 handlers = [
38 (r"/",MainHandler),
39 (r"/(.*)", NotFoundHandler),
40 ],
41 template_path = 'template', # 指定路径
42 static_path= 'static',
43 ui_methods=util.ui_methods, # 等号左侧是固定写法,不能变
44 ui_modules=util.ui_modules, # 等号左侧是固定写法,不能变
45 debug = True # 调试模式 文件修改后 自动重启服务器
46 )
47
48 if __name__ == '__main__':
49 print(options.port)
50 print(options.version)
51 tornado.options.parse_command_line()
52 # app = tornado.web.Application( handlers=[(r'/',MainHandler),] )
53 http_server = tornado.httpserver.HTTPServer(application)
54 http_server.listen(options.port) # 监听端口
55 tornado.ioloop.IOLoop.instance().start() #服务器自动循环 等待访问
extend04.html
1 {% extends base03.html %}<!--extends 继承base03.html,一般只继承一个 -->
2 {% block title %} base_300 {% end %}<!--block 修改block里的内容 -->
3
4 {% block body %}<!--block 修改block里的内容 -->
5 hello everyone !
6 {% include include05.html %} <!--include 包含(不是修改)include05.html 被导入的文件不能含extend 、block-->
7 <br>
8 {% import time %} <!--引入模块 -->
9 {{ time.time() }}
10 <br>
11 {{ haha() }}<!--调用函数 -->
12 <br>
13 {% from util.mod_file import adda,upper %}<!--调用自定义模块里的函数 -->
14 {{ adda(3,5) }}
15 <br>
16 {% module UiModule() %}<!--调用自定义模块里的类 -->
17 <br>
18 {{methods1()}}<!--调用自定义模块里的函数 -->
19 <br>
20 {% apply upper %} <!--使用函数的作用范围到最近的{%end%}为止 -->
21 lasdjflkdsjfl
22 {% end %}
23 <br>
24 {% raw linkify('百度raw版:http://www.baidu.com') %}<!--html解析 a标签 -->
25 <br>
26 {{ linkify('百度:http://www.baidu.com') }}<!--html不解析 a标签 -->
27 <br>
28 {{ adda(3,5) }}
29 <br>
30 {% module Advertisement() %} <!--案例 植入广告 -->
31
32
33 {% end %}
base03.html
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <title>{% block title %} base03 {% end %}</title>
6 <link rel="shortcut icon" href="{{ static_url('images/favicon.ico') }}">
7 <style>
8 div{
9 margin: 30px
10 }
11 </style>
12 </head>
13 <body>
14 <div>
15 {% block body %}
16 hello world !
17 {% end %}
18 </div>
19 </body>
20 </html>
include05.html
1 <br>
2 this is tornado include
3 <br>
mod_file.py
1 def adda(a,b):
2 return '乘积是:' + str(a*b)
3
4 def upper(a):
5 return a.upper()
ui_methods.py
1 '''
2
3 this is ui_method
4 '''
5
6 def methods1(self):
7 return 'ui_methods1'
ui_modules.py
1 '''
2 this is ui_modules
3
4 '''
5 from tornado.web import UIModule # 必须导入这个 UIModule 类
6
7 class UiModule(UIModule):# 固定继承类 UIModule
8
9 def render(self,*args,**kargs): #固定写法 render(self,*args,**kargs)
10 return '我是ui_modules'
11
12
13 ''' 案例:植入广告 '''
14 class Advertisement(UIModule):
15
16 def render(self, *args, **kwargs):
17 return self.render_string('06ad.html')
18
19 def css_files(self):
20 return "/static/css/King_Chance_Layer7.css"
21
22 def javascript_files(self):
23 return ["/static/js/jquery_1_7.js",
24 "/static/js/King_Chance_Layer.js",
25 "/static/js/King_layer_test.js",
26 ]