Flask 如何仅在UI的一部分接收输入时执行函数?

Flask 如何仅在UI的一部分接收输入时执行函数?,flask,Flask,到目前为止,我有这个 app = Flask(__name__) @app.route('/', methods=['GET', 'POST']) def first(): if request.method == 'GET': return render_template('index.html') if request.method == 'POST': body = request.get_json() text1 = bo

到目前为止,我有这个

app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def first():
    if request.method == 'GET':
        return render_template('index.html')
    if request.method == 'POST':
        body = request.get_json()

        text1 = body['text1 ']
        text2 = body['text2']
        someFunction(text1)
        otherFunction(text2)
但是,如果我只想在UI的某个部分接收输入时执行函数,该怎么办?假设只有当用户将某个内容放入
text2
框时,才会执行
otherFunction
。到目前为止,每次我发帖,都会被执行

所以我想我在找类似的东西

app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def first():
    if request.method == 'GET':
        return render_template('index.html')
    if request.method == 'POST':
        body = request.get_json()

        if request.method.box1:
            text1 = body['text1']
            someFunction(text1)
       if request.method.box1:
            text2 = body['text2']
            otherFunction(text2)

您几乎是对的,但是
request.method
没有
box1
属性

因此,这将是正确的方法:

app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def first():
    if request.method == 'GET':
        return render_template('index.html')
    if request.method == 'POST':
        body = request.get_json()

        if body.get('text1'):
            someFunction(body['text1'])
        if body.get('text2'):
            otherFunction(body['text2'])
app=Flask(\uuuuu name\uuuuuu)
@app.route('/',方法=['GET','POST'])
def first():
如果request.method==“GET”:
返回渲染模板('index.html')
如果request.method==“POST”:
body=request.get_json()
如果body.get('text1'):
someFunction(body['text1'])
如果body.get('text2'):
otherFunction(正文['text2'])