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

Unable to call flask method from another flask method of same python file

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

@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.

如何解决这个问题

Flask 函数需要一个 http 请求。您可以在没有 Flask app.route 装饰器的情况下创建命令函数。这将有助于分离功能和 http 请求。例如下面:

def add_employee():
    # Add Some Query here
    pass



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


@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.