先在web目录下新建一个tmplates的文件夹,然后在tmplates下再建一个index.html文件,打开index.html,输入以下内容: $def with (name) $if name: I just wanted to say <em>hello</em> to $name. $else: <em>Hello</em>, world! 其中$def with (name)表示模板文件将从这后面取值,前添加$符号的代表这句是服务端的代码。 更改一下昨天的code.py文件,更改为: import web #encoding=utf8 render = web.template.render('tmplates/') urls = ( '/', 'index' ) class index: def GET(self): i = web.input(name = None) return render.index(i.name) if __name__ == "__main__": app = web.application(urls, globals()) app.run() 其中web.template.render('tmplates/')表示web.py会在刚刚新建的tmplate目寻找的render.index,也就是刚刚新建这index.html,这里的名字必须一致,否则会抛出异常,大概的意思是说找不到文件之类的。 保存好退出,在浏览器里输入http://localhost:8080/?name=tcrct,这里域名后的参数需要写成/?key=value,这个?前的是URI,后的就是提交的参数值。与往常的不一致。 还有一种方式,取消?,&这种提交参数方式。将code.py改成如下的: import web #encoding=utf8 render = web.template.render('tmplates/') urls = ( '/(.*)', 'index' ) class index: def GET(self,name): #i = web.input(name = None) return render.index(name) if __name__ == "__main__": app = web.application(urls, globals()) app.run() 其中将urls={...} 里面的内容 再改/(.*),将GET()方法添加多一个变量,然后再直接发送到你的手机上。此时的URL应该是这样的:http://localhost:8080/tcrct
web.py模板
猜你喜欢
转载自tcrct.iteye.com/blog/1846099
今日推荐
周排行