打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
flask第二十篇——模板【3】

现在我们通过查询字符串的方式给render_template传参,我们就要用到flask库的flask.request.args.get()函数先获取参数,在index.html中给url_fornext,最后在login.html函数中通过{{ next }}传值。代码如下:

rendertemplateDemo.py文件

# coding: utf-8

from flask import Flask, render_template
import flask app = Flask(__name__)  # type: Flask
app.debug = True

@app.route('/')
def hello_world():    title = {"tPrize": "key",
            "info": {"name": u"Warren",
                     "age": 18,
                     "gender": u"男"},
            "val": {"title": u'标题',
                    "content": u'内容'}}
   return render_template('post/index.html', **title)


@app.route('/login/')
def login():    next = flask.request.args.get('next')
   return render_template('login.html', next=next)
   

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

index.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>这里是title</title>
</head>
<body>

{#    <p>这段代码被注释了</p>#}
    <p>{{ info }}</p>
    <a href="{{ url_for('login', next='首页') }}">链接到登录页面</a>
</body>
</html>

login.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录页面</title>
</head>
<body>
    这是登录页面,它来自{{ next }}。
</body>
</html>

然后执行代码,看到:

点击“链接到登录页面”后:

如果你想指定传值类型是path类型,那么就要给login函数传值了:
修改rendertemplateDemo.py文件的login函数如下:

@app.route('/login/<next>/')
def login(next):    # next = flask.request.args.get('next')    return render_template('login.html', next=next)

另外两个文件不变,然后执行代码:

点击“链接到登录页面”后:

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Python|Flask实现登录功能
超实用的Flask入门基础教程,新手必备!
chatgpt实践:用BitoAI写代码
Flask 教程,第二部分:模板
模板templates(17)
flask后台与前端(html)交互的两种方法
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服