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

運行效果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM