Issue
I am running into a problem trying to redirect in Flask using the following:
@views.route('/dash_monitoring/<path:url>')
@login_required
def monitoring_page(url):
return redirect("/home/dash_monitoring/{}".format(url))
The url in <path:url>
is in the format https://somesite.com/detail/?id=2102603
but when I try to print it in the function, it prints https://somesite.com/detail
only without the id part,so it obviously redirects to /home/dash_monitoring/https://somesite.com/detail
instead of /home/dash_monitoring/https://somesite.com/detail/?id=2102603
.
What should I do so it keeps the id part and redirects to the right url?
Solution
You can use request.url
and imply string manipulation:
@views.route('/dash_monitoring/<path:url>')
@login_required
def monitoring_page(url):
parsed_path = request.url.split('/dash_monitoring/')[1]
#return the parsed path
return redirect("/home/dash_monitoring/{}".format(parsed_path))
Alternatively, you can iterate through request.args
for creating query string and construct path with args
@views.route('/dash_monitoring/<path:url>')
@login_required
def monitoring_page(url):
query_string = ''
for arg,value in request.args.items():
query_string+=f"{arg}={value}&"
query_string=query_string[:-1] # to remove & at the end
path=f"{path}?{query_string}"
#return the parsed path
return redirect(f"/home/dash_monitoring/{path}")
I hope this helps :)
Answered By - ShivaGaire
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.