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

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文件,该文件内容为:

$def with (name)
$if name:
    I just wanted to say <em>hello</em> to $name.
$else:
    <em>Hello</em>, world!

3、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()

运行效果:

 

代码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()

运行效果:

posted @ 2014-08-14 07:28  小五义  阅读(15598)  评论(6编辑  收藏  举报