【问题标题】:How to pass arguments for return redirect(url()) in flask如何在烧瓶中传递返回重定向(url())的参数
【发布时间】:2020-04-05 08:43:07
【问题描述】:

我正在尝试更新烧瓶中的配置文件。一切正常,但是当我给出 {{ msg11 }} 时,一旦其编辑的 msg 没有显示在 html 页面中。我不知道如何传递参数以便将 msg 显示在 html 页面中。我尝试了这种方法,但它没有显示在 html 页面中

@app.route("/updateProfile", methods=["GET", "POST"])
def updateProfile():
    if request.method == 'POST':
        email = request.form['email']
        firstName = request.form['firstName']
        lastName = request.form['lastName']
        with sqlite3.connect('database.db') as con:
                try:
                    cur = con.cursor()
                    cur.execute('UPDATE users SET firstName = ?, lastName = ? WHERE email = ?', (firstName, lastName))
                    con.commit()
                    msg11 = "Saved Successfully"
                except:
                    con.rollback()
                    msg11 = "Error occured"
        con.close()
        return redirect(url_for('editProfile', msg11=msg11))

添加编辑配置文件功能。我已经删除了这个函数中的 sql 查询

def editProfile():
    if 'email' not in session:
        return redirect(url_for('root'))
    loggedIn, firstName, noOfItems = getLoginDetails()

        profileData = cur.fetchone()
    conn.close()
    return render_template("editProfile.html", profileData=profileData, loggedIn=loggedIn, firstName=firstName, noOfItems=noOfItems)

【问题讨论】:

  • editProfile 函数在哪里?
  • @IainShelvington 添加了editProfile函数

标签: python python-3.x flask jinja2


【解决方案1】:

当您向 url_for 函数提供关键字参数时,Flask 会自动将您的值编码为 URI 组件并将它们附加到您的请求 url。

以你的代码为例,这样做

msg11='Saved Successfully'
return redirect(url_for('editProfile', msg11=msg11))

将在传递给editProfile 路由时生成此 URL

/editProfile?msg11=Saved+Successfully

您可以使用 Flask 的内置 request 对象轻松提取此值

from flask import request
# Inside of editProfile() route
msg11 = request.args.get('msg11', None)

这样,您可以简单地检查该值是否存在(调用 get() 与通常的 Python 约定相同,如果未找到该值,将返回值 None)并将其传递给您的HTML 模板。

此外,使用 Jinja 的功能,只有当它不是 None 值时,您才可以使用类似这样的方式显示消息

{% if msg11 is not none %}
<h1>{{ msg11 }}</h1>
{% endif %}

下面是一个完整的最小示例

app.py

from flask import Flask, render_template, redirect, url_for, request

app = Flask(__name__)

@app.route("/updateProfile", methods=["GET", "POST"])
def updateProfile():
    msg11 = "Saved Successfully"
    return redirect(url_for("editProfile", msg11=msg11))

@app.route("/editProfile")
def editProfile():
    msg11 = request.args.get("msg11")
    return render_template("editProfile.html", msg11=msg11)

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

templates/editProfile.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>editProfile</title>
</head>
<body>
    {% if msg11 is not none %}
    <h1>{{ msg11 }}</h1>
    {% else %}
    <h1>msg11 has a value of None</h1>
    {% endif %}
</body>
</html>

【讨论】:

  • 它给出了一个错误 AttributeError: 'Request' object has no attribute 'get'。所以我尝试了 request.args.get 我的 url 变成了这样 /editProfile?msg11=Saved+Successfully
  • 是的@error_ab 你是完全正确的,我应该在发布之前发现这一点。
  • 有没有其他方法可以在html页面中显示msg??@vulpxn
  • 如果你的意思是不设置条件,你总是可以选择显示msg11无论如何,你只会看到一个nullNone在你的界面打印,但那是超出了所问问题的范围。
  • 我已经更新了我的答案,以包括我能够让它工作的完整最小示例,看看那个。
猜你喜欢
  • 1970-01-01
  • 2022-07-09
  • 1970-01-01
  • 2016-04-13
  • 2020-06-04
  • 2023-03-09
  • 1970-01-01
  • 2013-08-21
  • 2023-03-21
相关资源
最近更新 更多