瓶Python的按钮 [英] Flask Python Buttons

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

问题描述

我试着去在页面上创建两个按钮。每一个我想进行服务器上的不同的python脚本。到目前为止,我只设法/收集利用一个按钮

Im 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)

我需要改变的基础上按钮pressed?

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" value="Do Something">
<input type="submit" name="submit" value="Do Something Else">

然后在你的瓶视图功能,您能告诉哪个按钮是用来提交表单:

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'] == 'Do Something':
            pass # do something
        elif request.form['submit'] == '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天全站免登陆