如何在Python(Flask)中更改按钮的值

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

我想将用html编写的按钮的值更改为“记录”。我想解决并更改 python 中的按钮。这是为了防止在请求表单更改时点击记录按钮两次,并获得其有效的视觉反馈。

这就是我所拥有的:

@app.route('/camera', methods=['GET', 'POST'])
def camera(): 
    if request.method == 'POST':
        if request.form['record'] == 'record here' :  # <-------- here I ask what the value of the button 
            print ("is recording")                    #           is and it works fine
            p1 = threading.Thread(target=recording)
            p1.start()
    return render_template('camera.html')

这就是我想要的:

@app.route('/camera', methods=['GET', 'POST'])
def camera(): 
    if request.method == 'POST':
        if request.form['record'] == 'record here' :
            print ("is recording")
            p1 = threading.Thread(target=recording)
            p1.start()
            request.form['record'] = 'records' :     # <----------------- Something like this
    return render_template('camera.html')

我是 python html Flask 和 Web 开发的新手。

python html flask button
1个回答
0
投票

您想要做的事情应该通过 JavaScript 完成,因为更改按钮标签的最自然方法是通过客户端编程。

但是,如果您坚持从服务器端代码执行此操作,则可以从更改模板开始

camera.html
。假设您的模板中有以下按钮:

<input type="submit" name="record" value="record here">

应将其更改为包含变量:

<input type="submit" name="record" value="{{ record }}">

现在您可以使用

render_template
发送此变量的值:

@app.route('/camera', methods=['GET', 'POST'])
def camera(): 
    if request.method == 'POST':
        if request.form['record'] == 'record here' :
            print ("is recording")
            p1 = threading.Thread(target=recording)
            p1.start()
            return render_template('camera.html', record='records')
    return render_template('camera.html', record='record here')

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