小编典典

flask Python按钮

flask

我试图在页面上创建两个按钮。我想在服务器上执行每个不同的python脚本。到目前为止,我仅设法使用

def contact():
  form = ContactForm()

  if request.method == 'POST':
    return 'Form posted.'

  elif request.method == 'GET':
     return render_template('contact.html', form=form)

我需要根据按下的按钮进行哪些更改?


阅读 2120

收藏
2020-04-06

共1个答案

小编典典

为两个按钮指定相同的名称和不同的值:

<input type="submit" name="submit_button" value="Do Something">
<input type="submit" name="submit_button" value="Do Something Else">

然后,在Flask视图函数中,您可以知道使用了哪个按钮来提交表单:

def contact():
    if request.method == 'POST':
        if request.form['submit_button'] == 'Do Something':
            pass # do something
        elif request.form['submit_button'] == 'Do Something Else':
            pass # do something else
        else:
            pass # unknown
    elif request.method == 'GET':
        return render_template('contact.html', form=form)
2020-04-06