gpt4 book ai didi

python - 在 Flask (Python) 中重定向到包含 'variable part' 的 URL

转载 作者:太空狗 更新时间:2023-10-29 21:26:56 24 4
gpt4 key购买 nike

我正在尝试重定向到 Flask 中的 URL。我尝试重定向到的目标 URL 有一个像这样的变量 /dashboard/<username>其 View 如下,

@app.route('/dashboard/<username>')
def dashboard(username):
return render_template('dashboard.html', username=username)

如何使用 Flask 的 redirect() 重定向到此 URL & url_for()功能。这个我试过了,

return redirect(url_for("index"))

工作正常,因为索引是我的应用程序中没有任何可变部分 ( /index) 的 URL。但是,我该如何处理具有可变路径的 URL?

谢谢

最佳答案

您需要使用 url_for 创建您的 URL,方法是按照以下方式为其提供您的 URL 名称、关键字 arg 和 URL 参数的值:

return redirect(url_for('dashboard', username='foo'))

关于python - 在 Flask (Python) 中重定向到包含 'variable part' 的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16081754/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com