无法从同一python文件的另一个flask方法调用flask方法

问题描述 投票:0回答:1

无法从相同python文件的另一个flask方法调用flask方法。例如,考虑下面的代码

@app.route('/api/v1/employee/add', method = ['POST'])
def add_employee():
    req = request.json
    #add the employee to db

@app.route('/api/v1/employee', method = ['GET'])
def get_employee():
    data = json.loads(employee) #getting employee details from db
    add_employee() # unable to call this method as I am getting the data as null.

如何解决此问题

python python-3.x flask flask-restful
1个回答
0
投票
Flask Function将请求一个http请求。您可以在没有Flask app.route装饰器的情况下创建命令功能。这将有助于分隔功能和http请求。例如,以下示例:

def add_employee(): @app.route('/api/v1/employee/add', method = ['POST']) def http_employee(): req = request.json add_employee() #add the employee to db @app.route('/api/v1/employee', method = ['GET']) def get_employee(): data = json.loads(employee) #getting employee details from db add_employee() # unable to call this method as I am getting the data as null.

© www.soinside.com 2019 - 2024. All rights reserved.