上手的第一个web.py的例子
总是出现:
no template named hello
以下是解决的笔记:
web.py 需要调用templates
web.py的应用需要在/下建立templates文件夹
里面放需要被调用的html文件,比如 hello.html , index.html等等
在html可以写简单的代码,注意缩进要求严格如python
以下是参考过的内容:
https://www.oschina.net/question/5189_4306
在Python 里面编写HTML 代码是相当累赘的,而在HTML 里嵌入Python 代码则有趣得多。幸运地,web.py 使这过程变得相当容易。
注意:旧版本的web.py 是用Cheetah templates 模板的,你可以继续使用,但官方已不再提供支持。
在我们的web 应用里,添加一个新的文件夹用来组织模板文件(例如”/templates“)。然后再新建一个HTML 文件(例如:”index.html“):
<em>Hello</em>, world!
或者,你可以用web.py 的模板语言用编写这个HTML 文件:
$def with (name)
$if name:
I just wanted to say <em>hello</em> to $name.
$else:
<em>Hello</em>, world!
注意上面代码的缩进!
正如你所见的,上面的模板看上去跟这Python 文件很相似,以def with 语句开始,但在关键字前需要添加”$“。
注意:在模板内的变量,如果包含有HTML 标记,以$ 方式引用变量的话,HTML 标记只会以纯文本的显示出来。要想HTML 标记产生效果,可以用$: 引用变量。
http://www.itgo.me/a/x4110729436096248118/web.py
一、web.py简介
web.py是一款轻量级的python web开发框架,简单、高效、学习成本低,特别适合作为python web开发的入门框架。官方站点:http://webpy.org/
二、web.py安装
1、下载:http://webpy.org/static/web.py-0.33.tar.gz
2、解压并进入web.py-0.33目录,安装:python setup.py install
入门例子,hello.py
import web
urls = (
'/', 'index'
)
class index:
def GET(self):
print "Hello, world!"
web.webapi.internalerror = web.debugerror
if __name__ == "__main__": web.run(urls, globals(), web.reloader)
模板例子:
1.创建 template.py,内容如下:
import web
render = web.template.render('templates/')
urls =
(
相似文章推荐:RESTful Web - a0z
本文是Horizon第二次线下活动中“RESTful Web”讲题的总结,由PengEdy整理。
REST的全称是Representational State Transfer,可译为“表征状态转移”,是Roy Fielding博士在2000年他的博士论文中提出的一种软件架构风格。目前,在三中主流的Web服务实现方式
'/(.*)', 'index')
class index:
def GET(self,name):
print render.index(name)
web.webapi.internalerror = web.debugerror
if __name__ == "__main__": web.run(urls, globals(), web.reloader)
2.在template.py同级目录下,创建templates目录
3.在templates目录下创建index.html
$def with (name)
$if name:
I just wanted to say hello to $name.
$else:
Hello, world!
4. 启动程序
$ python template.py