【问题标题】:Python + Flask, post button using namePython + Flask,使用名称发布按钮
【发布时间】:2018-01-24 20:55:27
【问题描述】:

我有三个按钮,例如:

<form method="post"> 
<input type="submit" name="one" class="btn btn-danger" value="Delete"> 
<input type="submit" name="two" class="btn btn-danger" value="Delete"> 
<input type="submit" name="three" class="btn btn-danger" value="Delete"> 
</form>

有没有办法在不使用 name="submit" 的情况下发布按钮

我正在使用类似的帖子

 if request.method == "POST":
  if request.form['submit'] == 'Delete':

但它只适用于

<input type="submit" name="submit" class="btn btn-danger" value="Delete">

我试过了:

if request.form['one'] == 'Delete':

但是什么也没发生。

【问题讨论】:

  • 你的意思是不使用 type=submit ?
  • if request.form['submit'] == 'Delete': (for name="submit) 它有效,但我想为 "name="one" 创建帖子
  • type=submit 必须保持它是按钮提交
  • 它们是同一个表格还是不同表格?请张贴表格代码
  • 跨度>

标签: python flask


【解决方案1】:

表单用于接收来自用户的输入并使用后端脚本对其进行处理。但是,您似乎没有接受用户输入,而是使用提交标签和按钮。无需使用表单来触发操作,只需使用三个 hrefs 或三个带有 onlick 操作的按钮:

选项1:三个hrefs 重定向到特定路由进行操作:

<a href='/my_action1' class="btn btn-danger">Delete</a>
<a href='/my_action2' class="btn btn-danger">Delete</a>
<a href='/my_action3' class="btn btn-danger">Delete</a>

现在,在您的主烧瓶应用程序文件中,创建处理操作的路由:

@app.route('/my_action1')
def do_something1():
  return
@app.route('/my_action2')
def do_something2():
  return
@app.route('/my_action3')
def do_something3():
  return

选项 2:三个带有 onclick 的按钮:

<button type="button" class="btn btn-success" onclick='do_something()'>Delete</button>
<button type="button" class="btn btn-success" onclick='do_something1()'>Delete</button>
<button type="button" class="btn btn-success" onclick='do_something2()'>Delete</button>

&lt;script&gt;:

function do_something(){
  //perform specific operation
  alert('something');
}
function do_something1(){
  //perform specific operation
  alert('something1');
}
function do_something2(){
  //perform specific operation
  alert('something2');
}

【讨论】:

    猜你喜欢
    • 2016-12-15
    • 2017-06-23
    • 2015-09-03
    • 2015-03-21
    • 1970-01-01
    • 1970-01-01
    • 2017-08-06
    • 1970-01-01
    • 2017-12-09
    相关资源
    最近更新 更多