《python网络编程学习笔记(10):webpy框架》(http://www.cnblogs.com/xiaowuyi/archive/2012/11/15/2771099.html#3006443)的解释。html
网友@etfengyun近期提出疑问,在webpy0.33上利用模板时出现错误。因为我按@etfengyun的做法没有再现出错误,因此很差判断错误的缘由,这里把具体利用模板的步骤再详细解释一下。python
一、环境:python2.7.x+webpy0.33(下载地址:http://webpy.org/static/web.py-0.33.tar.gz)web
二、创建test文件夹,将webpy0.33解压出来的web文件夹存在放在test下,并创建testwebpy.py文件以及创建templates文件夹,在templates文件夹下,创建index.html文件,该文件内容为:编程
$def with (name) $if name: I just wanted to say <em>hello</em> to $name. $else: <em>Hello</em>, world!
三、testwebpy.py的代码:网络
##@小五义http://www.cnblogs.com/xiaowuyi import web render = web.template.render('templates/') urls = ( '/', 'index' ) class index: def GET(self): name='Bob' return render.index(name) #return "Hello, world!" if __name__ == "__main__": app = web.application(urls, globals()) app.run()
运行效果:app
代码2:框架
##@小五义http://www.cnblogs.com/xiaowuyi import web render = web.template.render('templates/') urls = ( '/(.*)', 'index' ) class index: def GET(self,name): i=web.input(name=None) return render.index(name) #return "Hello, world!" if __name__ == "__main__": app = web.application(urls, globals()) app.run()
运行效果:python2.7