Flask Python-如何将变量从一种方法传递到另一种方法

时间:2020-07-15 09:35:41

标签: python function flask

我有一个flask应用程序,我需要将变量从一个函数传递给另一个函数。这里的要求是我需要将index()中的'skill'值传递给get_image(),有关如何实现的任何想法?

@app.route('/', methods=['POST'])
def index():
    data = json.loads(request.get_data())
    skill = data['conversation']['skill']


@app.route("/get-image/<image_name>")
def get_image(image_name):
    if skill == 'getpositions':
        # some code

1 个答案:

答案 0 :(得分:2)

例如,您可以尝试使用flask sessions

from flask import Flask, session

...

@app.route('/', methods=['POST'])
def index():
    data = json.loads(request.get_data())
    skill = data['conversation']['skill']
    session['skill'] = skill

@app.route("/get-image/<image_name>")
def get_image(image_name):
    if session['skill'] == 'getpositions':
    #some code

检查documentation