sagar_acharya
sagar_acharya

Reputation: 366

How to pass username through an href in a flask app?

In the code below, there are 2 possible ways. If the user is logged in, he has cookie stored which will be checked and voila, a different html will appear for him. However, I want to pass username through a link from previous page.

@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)

Below is the html link to the above route function in flask. The username printed above is always None despite the browser bar showing ___/doc?username=maman

<a href ="{{url_for('doc', username=username)}}">Documentation</a>

What am I doing wrong?

Upvotes: 1

Views: 502

Answers (1)

Raj Srujan Jalem
Raj Srujan Jalem

Reputation: 661

You are reading the parameters in a wrong way. This would work:

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

Upvotes: 1

Related Questions