码迷,mamicode.com
首页 > 其他好文 > 详细

Flask 的模板渲染

时间:2020-03-18 23:39:13      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:安全   doctype   safe   hello   千万   道路   ret   world   use   

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>


Flask 的模板渲染

标签:安全   doctype   safe   hello   千万   道路   ret   world   use   

原文地址:https://www.cnblogs.com/kai-/p/12521120.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!