user6702954
user6702954

Reputation: 85

passing an argument from flask template to view

I am trying to pass an argument from my template as shown below:

<form action="/removesessions/?userid={{userid}}" method="post">
    <button size="5" style="height:40px;width:170px" name="forwardBtn" type="submit">Remove Idle Sessions<font size="5"></button>
</form>

To my view below but can't get it to work.

@app.route("/removesessions/",methods=["GET","POST"])
def removesessions(userid):
    print(userid)

can someone tell me how can I get it to work. thanks..

Upvotes: 0

Views: 35

Answers (1)

vremes
vremes

Reputation: 674

If you want to access the query parameter userid in Flask, you can use request.args.get('userid') or request.args['userid'].

Example code:

@app.route("/removesessions/", methods=["GET", "POST"])
def removesessions():
    userid = request.args.get('userid') # or userid = request.args['userid']
    print(userid)

More information can be found in Flask docs: https://flask.palletsprojects.com/en/1.1.x/api/#flask.Request.args

Upvotes: 1

Related Questions