python web py入门(3)-模板
2018-02-08 14:14
169 查看
WEB服务器在生成HTML网页时,有两个选择,一个是从代码里嵌入HTML来生成HTML页面返回,另一个是从HTML代码里嵌入执行代码来生成HTML页面返回。这两种方式,其实都有使用,比如在嵌入式系统里,往往就是使用C或C++来生成HTML页面,这些是嵌入在C++代码里面的。但是这种方式是有它的缺点的,比如一个淘宝上页面,其实很多图片资源和网页的样式是固定的,只要少部分数据需要从数据库里找出来,再填充生成的,这时变动的数据非常小,就可以把固定的数据变成一个模块。当需要修改图片资源时,就不方便了。
同样,在 Python 中写 HTML 不是聪明的选择,相反在 HTML 中写 Python 则有趣的多。幸运的是,web.py 让这件事情做得简单而又漂亮。
给模板新建一个目录(命名为 templates),在该目录下新建一个以 .html 结尾的文件,内容如下:
<em>Hello</em>, world!
你也可以在模板中使用 web.py 模板支持代码:
$def with (name)
$if name:
I just wanted to say <em>hello</em> to $name.
$else:
<em>Hello</em>, world!
如上,该模板看起来就像 python 文件一样,除了顶部的 def with (表示从模板将从这后面取值)和总是位于代码段之前的$。当前,template.py 首先请求模板文件的首行 $def 。当然,你要注意 web.py 将会转义任何任何用到的变量,所以当你将 name 的值设为是一段 HTML 时,它会被转义显示成纯文本。如果要关闭该选项,可以写成 $:name 来代替 $name。
回看再看 TestTemplate.py。在第一行之下添加:
render = web.template.render('templates/')
这会告诉web.py到你的模板目录中去查找模板。然后把 index.GET改成: 告诉 web.py 在你的模板目录下查找模板文件。修改 index.GET :
name = 'Bob'
return render.index(name)
('index' 是模板的名字,'name' 是传入模板的一个参数)
访问站点它将显示 hello Bob。
但是如果我们想让用户自行输入他的名字,么办?如下:
i = web.input(name=None)
return render.index(i.name)
访问 / 将显示 hello world,访问 /?name=Joe 将显示 hello Joe。
URL 的后面的 ? 看起来不好看?修改下 URL 配置:
'/(.*)', 'index'
然后修改下 index.GET:
def GET(self, name):
return render.index(name)
现在访问 /Joe 看看,它会显示 hello Joe。
例子的源码:
#python 3.6
#蔡军生
#http://blog.csdn.net/caimouse/article/details/51749579
#
import web
urls = (
'/(.*)', 'index'
)
app = web.application(urls, globals())
render = web.template.render('templates/')
class index:
def GET(self, name):
i = web.input(name=None)
return render.index(i.name)
if __name__ == "__main__":
app.run()需要模板的目录里创建一个文件sample\templates\index.html,它内容如下:
$def with (name)
$if name:
I just wanted to say <em>hello</em> to $name.
$else:
<em>Hello</em>, world!
然后运行程序之后,就可以在浏览器里输入连接地址:http://127.0.0.1:8080/?name=caimouse, 输出结果如下:
比特币源码入门教程
https://edu.csdn.net/course/detail/6998
深入浅出Matplotlib
深入浅出Numpy
同样,在 Python 中写 HTML 不是聪明的选择,相反在 HTML 中写 Python 则有趣的多。幸运的是,web.py 让这件事情做得简单而又漂亮。
给模板新建一个目录(命名为 templates),在该目录下新建一个以 .html 结尾的文件,内容如下:
<em>Hello</em>, world!
你也可以在模板中使用 web.py 模板支持代码:
$def with (name)
$if name:
I just wanted to say <em>hello</em> to $name.
$else:
<em>Hello</em>, world!
如上,该模板看起来就像 python 文件一样,除了顶部的 def with (表示从模板将从这后面取值)和总是位于代码段之前的$。当前,template.py 首先请求模板文件的首行 $def 。当然,你要注意 web.py 将会转义任何任何用到的变量,所以当你将 name 的值设为是一段 HTML 时,它会被转义显示成纯文本。如果要关闭该选项,可以写成 $:name 来代替 $name。
回看再看 TestTemplate.py。在第一行之下添加:
render = web.template.render('templates/')
这会告诉web.py到你的模板目录中去查找模板。然后把 index.GET改成: 告诉 web.py 在你的模板目录下查找模板文件。修改 index.GET :
name = 'Bob'
return render.index(name)
('index' 是模板的名字,'name' 是传入模板的一个参数)
访问站点它将显示 hello Bob。
但是如果我们想让用户自行输入他的名字,么办?如下:
i = web.input(name=None)
return render.index(i.name)
访问 / 将显示 hello world,访问 /?name=Joe 将显示 hello Joe。
URL 的后面的 ? 看起来不好看?修改下 URL 配置:
'/(.*)', 'index'
然后修改下 index.GET:
def GET(self, name):
return render.index(name)
现在访问 /Joe 看看,它会显示 hello Joe。
例子的源码:
#python 3.6
#蔡军生
#http://blog.csdn.net/caimouse/article/details/51749579
#
import web
urls = (
'/(.*)', 'index'
)
app = web.application(urls, globals())
render = web.template.render('templates/')
class index:
def GET(self, name):
i = web.input(name=None)
return render.index(i.name)
if __name__ == "__main__":
app.run()需要模板的目录里创建一个文件sample\templates\index.html,它内容如下:
$def with (name)
$if name:
I just wanted to say <em>hello</em> to $name.
$else:
<em>Hello</em>, world!
然后运行程序之后,就可以在浏览器里输入连接地址:http://127.0.0.1:8080/?name=caimouse, 输出结果如下:
比特币源码入门教程
https://edu.csdn.net/course/detail/6998
深入浅出Matplotlib
https://edu.csdn.net/course/detail/6859
深入浅出Numpy
http://edu.csdn.net/course/detail/6149
Python游戏开发入门
http://edu.csdn.net/course/detail/5690相关文章推荐
- python web py入门(6)-webpy在模板里使用code代码段错误的问题
- Python基于回溯法子集树模板解决野人与传教士问题示例
- python web py入门(8)- 论坛的首页文章来源
- 修改python新建文件时的模板
- 用python写自定义模板
- python 利用模板文件生成配置文件
- python+django(模板)
- python如何直接在命令行使用系统模板template
- Python第十三天 django 1.6 导入模板 定义数据模型 访问数据库 GET和POST方法 SimpleCMDB项目 urllib模块 urllib2模块 httplib模块 django和web服务器整合 wsgi模块 gunicorn模块
- Python基于回溯法子集树模板解决全排列问题示例
- 『Python』 ThreadPool 线程池模板
- Python导入其他目录的模板
- python 多线程模板
- python中使用sys模板和logging模块获取行号和函数名的方法
- python-Django 设置模板路径(九)
- python日志处理脚本模板
- Python基于回溯法子集树模板解决找零问题示例
- Saltstack API以及对应的Python模板
- [Python标准库]string——文本常量和模板
- python jinjia2模板使用