7、向模板传递变量

主页模板文件 index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>这是首页</title>
</head>
<body>
    <h1>这是首页中文字!</h1>
</body>
</html>

用户模板文件 user.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>这是用户中心</title>
</head>
<body>
  <h1>欢迎您:{{name}} </h1> {# 变量语法 #}
</body>
</html>

主程序文件

from flask import Flask
from flask import render_template
app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/user/<username>')
def user(username):
  # 在本函数该变量名为 username,在模板文件中名为 name。并且传递的变量数量无限制
    return render_template('user.html', name=username)

if __name__ == '__main__':
    app.run(debug=True)

参考资料

https://weread.qq.com/web/reader/0a932660718ac6bc0a9702e

本文作者:tiansz

本文链接:https://www.cnblogs.com/tiansz/p/16324345.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   tiansz  阅读(28)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起