【问题标题】:How to call self method and get data in flask?如何调用 self 方法并在烧瓶中获取数据?
【发布时间】:2019-12-30 02:36:50
【问题描述】:

我想从另一个 app.route 方法调用 app.route 方法。例如,代码看起来很简单。我想在 postMethod() 函数中调用 getMethod() 函数。我该怎么做?

@app.route('/ex1')
def getMethod():
    return "some value"

@app.route('/ex2', methods=['POST'])
def postMethod():
    # want to call getMethod() and get the return value
    # save data to database
    return jsonify('created'), 201

【问题讨论】:

  • 将其作为正常功能运行getMethod()。或者你在两个函数中需要的代码放在单独的函数中并在getMethod()postMethod()中运行这个函数

标签: python api flask


【解决方案1】:

解决方案here

一个可能的解决方案是创建一个没有装饰器的普通函数,然后在您想要的不同位置调用它。

@app.route('/ex1')
def getMethod():
    return commonMethod()

def commonMethod():
    return "some value"

@app.route('/ex2', methods=['POST'])
def postMethod():
    result = commonMethod()
    return jsonify('created'), 201

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多