1. 题目要求
写一个Hello,template程序,使用模板来显示Hello,{name},其中name是变量,要由外部传入
2. 框架说明
web.py (http://webpy.org)
3. 步骤
在命令行下操作
3.1. 1. 创建Project2项目
mkdir project2
3.2. 2. 创建Hello App
cd project2 vi hello.py
1
2 import web
3 from mako.template import TemplateLookup
4
5 lookup = TemplateLookup('templates')
6
7 def render(template, **kwargs):
8 return lookup.get_template(template).render(**kwargs)
9
10 urls = ('/', 'hello')
11
12 class hello:
13 def GET(self):
14 return render('hello.mako', name='template')
15
16 app = web.application(urls, globals())
17
18 if __name__ == '__main__':
19 app.run()
3.3. 4. 创建模板文件
cd project2 mkdir templates vi hello.mako
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>Hello World</title> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> </head> <body> <h1>Hello, ${name}</h1> </body> </html>
3.4. 5. 结束
4. 测试
cd project2 python hello.py
访问 http://localhost:8080 即可。