I have some view functions within a Blueprint. They are like following:
@app.route('/panel/<int:id>', methods=['GET'])
def get_panel(id):
panel = Panel.query.filter_by(id=id).first()
return jsonify(panel.getJson())
@app.route('/panel/<int:id>', methods=['POST'])
def post_panel(id):
panel = request.get_json().get('panel')
# code for saving the data in database
return jsonify({"message": "Saved in database"})
When I try to test the view function post_panel(), it somehow picks up the get_panel(). As both functions url are same and I think that's what causing the problem.
Is there any way around?
It's not possible to declare two functions to handle GET and POST separately. You must use one function, and a conditional to decide what to do, as demonstrated in this example in the docs:
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
do_the_login()
else:
show_the_login_form()
So you could write:
@app.route('/panel/<int:id>', methods=['GET', 'POST'])
def handle_panel(id):
if request.method == 'POST':
return post_panel(id)
else:
return get_panel(id)
def get_panel(id):
panel = Panel.query.filter_by(id=id).first()
return jsonify(panel.getJson())
def post_panel(id):
panel = request.get_json().get('panel')
# code for saving the data in database
return jsonify({"message": "Saved in database"})