标签:
模版变量
from flask import Flask, render_template app = Flask(__name__) @app.route(‘/‘) def index(): user=‘valentine‘ return render_template(‘index.html‘,username=user) if __name__ == ‘__main__‘: app.debug = True app.run()
在index.py同级目录建立templates文件夹,下新建index.html
<!doctype html>
<html>
<head>
<title>index</title>
</head>
<body>
<h1>Welcome, {{username}}!</h1>
</body>
<footer>
</footer>
</html>
{{变量}},将变量传递给html。
模版标签
#coding:utf-8 from flask import Flask, render_template app = Flask(__name__) @app.route(‘/‘) def index(): user=‘valentine‘ nav_list=[u‘首页‘,u‘经济‘,u‘文化‘,u‘科技‘,u‘娱乐‘] return render_template(‘index.html‘,username=user,nav_list=nav_list) if __name__ == ‘__main__‘: app.debug = True app.run()
传入nav_list参数,因为python不支持ascii码,所以在第一行加上。
<!doctype html>
<html>
<head>
<title>index</title>
</head>
<body>
<h1>Welcome, {{username}}!</h1>
{%for nav in nav_list%}
<li>{{nav}}</li>
{%endfor%}
</body>
<footer>
</footer>
</html>
for循环
标签:
原文地址:http://www.cnblogs.com/valentineisme/p/4280008.html