【问题标题】:How to pass username through an href in a flask app?如何通过烧瓶应用程序中的href传递用户名?
【发布时间】:2020-05-23 18:24:32
【问题描述】:

在下面的代码中,有两种可能的方式。如果用户已登录,他存储了 cookie,将对其进行检查,瞧,将为他显示一个不同的 html。但是,我想通过上一页的链接传递用户名。

@app.route("/doc")
def doc(username=None):
    print(username)              #shows None
    if username is not None:
        if verify_token(request.cookies['customCookie'+username], username):
            return render_template("doc.html", username=username)
        else:
            return redirect(url_for("login", msg="Session expired. Please login again"))
    return render_template("doc.html", username = username)

下面是flask中上述路由函数的html链接。尽管浏览器栏显示 ___/doc?username=maman

,上面打印的用户名始终为 None
<a href ="{{url_for('doc', username=username)}}">Documentation</a>

我做错了什么?

【问题讨论】:

  • 在回答你的问题之前,你使用的是flask-login吗?
  • 做:username = request.args.get('username')
  • 不,不使用烧瓶登录
  • 谢谢,@mechanical_meat
  • @caissalover:不客气!

标签: python html web flask


【解决方案1】:

您以错误的方式读取参数。这会起作用:

@app.route("/doc")
def doc():
    username = request.args.get('username')
    print(username)              
    if username is not None:
    ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 2019-04-05
    • 2018-11-08
    • 1970-01-01
    • 2017-08-20
    相关资源
    最近更新 更多