《python網(wǎng)絡(luò)編程學(xué)習(xí)筆記(10):webpy框架》(http://www.survivalescaperooms.com/xiaowuyi/archive/2012/11/15/2771099.html#3006443)的解釋。
網(wǎng)友@etfengyun近期提出疑問,在webpy0.33上利用模板時(shí)出現(xiàn)錯(cuò)誤。由于我按@etfengyun的作法沒有再現(xiàn)出錯(cuò)誤,所以不好判斷錯(cuò)誤的原因,這里把具體利用模板的步驟再詳細(xì)解釋一下。
1、環(huán)境: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文件,該文件內(nèi)容為:
$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.survivalescaperooms.com/xiaowuyiimport webrender = 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()
運(yùn)行效果:


代碼2:
##@小五義http://www.survivalescaperooms.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()
運(yùn)行效果:


新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注