Python web使用template模板

在启动文件hello_web.py同级目录下建立文件夹templates,以下图css

将html文件放进templates文件夹下。html

启动文件hello_web.py代码为:python

import web
d=[{"id":90,"name":'xianv'},{"id":98,"name":'liang'}]
urls = ("/hello", "hello",
        "/login", "login",)
app = web.application(urls, globals())
render = web.template.render('templates/',cache=False)
class hello:
    def GET(self):
        return render.index(d)
    
class login:
    def GET(self):
        return render.login(d)
  
if __name__ == "__main__":
    app.run()

以后运行hello_web.py。web

在网页中输入:浏览器

 http://127.0.0.1:8080/hello app

便可在网页中看到index.html实现:url

 

 

index.html代码:code

$def with (d)
<html>
<head>
<style type="text/css">
p 
{
border-style: solid;
border-top-color: #ff0000
}
</style>
<title>个人第一个 HTML 页面</title>
</head>
<body>
<p>$d[0]["id"]:$d[1]["name"]</p>
<p>$d[0]["id"]:$d[0]["name"]</p>
<p>$d[1]["id"]:$d[1]["name"]</p>
<p>$d[0]:$d[1]</p>
$for i in d:
 <p>$i["id"]:$i["name"]</p>
<p>title 元素的内容会显示在浏览器的标题栏中。</p>
</body>
</html>
相关文章
相关标签/搜索