烧瓶 Python 按钮 [英] Flask Python Buttons

查看:36
本文介绍了烧瓶 Python 按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在页面上创建两个按钮.每一个我都想在服务器上执行一个不同的 Python 脚本.到目前为止,我只能使用获取/收集一个按钮.

I'm trying to create two buttons on a page. Each one I would like to carry out a different Python script on the server. So far I have only managed to get/collect one button using.

def contact():
  form = ContactForm()

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

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

根据按下的按钮,我需要更改什么?

What would I need to change based on button pressed?

推荐答案

给你的两个按钮相同的名称和不同的值:

Give your two buttons the same name and different values:

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

然后在你的 Flask 视图函数中你可以知道哪个按钮用于提交表单:

Then in your Flask view function you can tell which button was used to submit the form:

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)

这篇关于烧瓶 Python 按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆