Issue
I am working on an app, and I want to send some parameters to render a page properly.
Here is my Flask code:
@app.get('/main')
def interface():
if True: # here supposed to be a function to check authorization
s = 'HELLO' # for testing purposes I hardcode the dummy parameter
return render_template('main.html', sid=s)
return redirect('/') # if not authorized, move home
When I run the app and try to open 127.0.0.1:5000/main , the parameter is not passed. I can see it in the developer console:
However if I write the address manually, like 127.0.0.1:5000/main?sid=HELLO, I see this:
What am I doing wrong?
Solution
The render_template('main.html', sid=s)
means Flask will respond to the the /main
path with the contents from the main.html template, and to generate that template the sid=HELLO will be used.
If you want Flask to redirect to a different path with a GET variable in the path, you would have to use url_for
, example:
return redirect(url_for("user.logout", user="admin")
See these two routes that may explain:
@main_blueprint.route("/login")
def login():
# lets assume your login processing determines the credentials submitted are for admin:
user = "admin"
return redirect(url_for("main_blueprint.user", id=user))
@main_blueprint.route("/user/<id>")
def user(id):
return "welcome user: {}".format(id)
by doing a GET to /login
, you will be redirected to /user/admin
.
Answered By - ilias-sp
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.