Flask 的模板渲染
from flask import Flask,render_template,Markup app = Flask(__name__) app.debug = True USERS = { 1:{'name':'张三','age':18,'gender':'男','text':"道路千万条"}, 2:{'name':'李四','age':28,'gender':'男','text':"安全第一条"}, 3:{'name':'王五','age':18,'gender':'女','text':"行车不规范"}, } def func1(arg,tank): return Markup(f"<h1>Hello World,{arg} is sb {tank} is same as {arg}</h1>") @app.route("/") def index(): # data = { # "user" :USERS, # "name": "santa" # } return render_template("index.html",user = USERS,name="santa",ht1 = func1,ht="<h1>Hello World1</h1>") #return render_template("index.html",**data) if __name__ == '__main__': app.run()
html 文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1> 我是html</h1> <table> {% for k,v in user.items() %} <tr> <td>{{ k }}</td> <td>{{ v.name }}</td> <td>{{ v['name'] }}</td> <td>{{ v.get('name') }}</td> <td>{{url_for("index")}}</td> </tr> {% endfor %} </table> <div>{{name}}</div> {% if name == "santa" %} <h1>666</h1> {% else %} <h1>水哥</h1> {% endif %} {{ ht|safe}} {{ht1("kai","santa")}} </body> </html>
原文地址:https://www.cnblogs.com/kai-/p/12521120.html
时间: 2024-11-09 06:24:37