web.py利用模板的详细步骤

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

《python网络编程学习笔记(10):webpy框架》(http://www.cnblogs.com/xiaowuyi/archive/2012/11/15/2771099.html#3006443)的解释。

网友@etfengyun近期提出疑问,在webpy0.33上利用模板时出现错误。由于我按@etfengyun的作法没有再现出错误,所以不好判断错误的原因,这里把具体利用模板的步骤再详细解释一下。

1、环境:python2.7.x+webpy0.33(下载地址:http://webpy.org/static/web.py-0.33.tar.gz)

2、建立test文件夹,将webpy0.33解压出来的web文件夹存在放在test下,并建立testwebpy.py文件以及建立templates文件夹,在templates文件夹下,建立index.html文件,该文件内容为:

$defwith (name)

$ifname:

I just wanted to say<em>hello</em>to $name.

$else:<em>Hello</em>, world!

3、testwebpy.py的代码:

##@小五义http://www.cnblogs.com/xiaowuyiimportweb

render= web.template.render('templates/')

urls=('/','index')classindex:defGET(self):

name='Bob'returnrender.index(name)#return "Hello, world!"if__name__=="__main__":

app=web.application(urls, globals())

app.run()

运行效果:

代码2:

##@小五义http://www.cnblogs.com/xiaowuyiimportweb

render= web.template.render('templates/')

urls=('/(.*)','index')classindex:defGET(self,name):

i=web.input(name=None)returnrender.index(name)#return "Hello, world!"if__name__=="__main__":

app=web.application(urls, globals())

app.run()

运行效果:

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航