Issue
I have a Flask application that I want to have two routes. I want to do after request work for both of them and have one start once the other finished.
@blueprint_main.route('/route1', methods=['POST', 'GET'])
def func1():
<do a few setup activities to give response>
return redirect ('/route2')
@blueprint_main.after_request()
def post_func1(response)
if request.path == '/route1':
<do the first main activities>
return response
@blueprint_main.route('/route1', methods=['POST', 'GET'])
def func2():
<do a few setup activities>
return <some response>
@blueprint_main.after_request()
def post_func2(response)
if request.path == '/route2':
<do the last main activities>
return response
However, my application seems to be getting stuck in the after_request
for route2
. Is this expected, and if so, is there a way to force Flask to do the after_request
work for func1
before starting the after_request
work for func2
?
Solution
You can merge all after_request
functions to one and switch by route to avoid stuck.
@blueprint_main.route('/route1', methods=['POST', 'GET'])
def func1():
<do a few setup activities to give response>
return redirect ('/route2')
@blueprint_main.route('/route2', methods=['POST', 'GET'])
def func2():
<do a few setup activities>
return <some response>
def post_func1(response):
<do function 1>
return response
def post_func2(response):
<do function 2>
return response
@blueprint_main.after_request
def post_func(response)
if request.endpoint in ["blueprint_main.func1"]:
response = post_func1(response)
elif request.endpoint in ["blueprint_main.func2"]:
response = post_func2(response)
return response
Answered By - TanThien
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.