如何在一个Flask函数中从单个页面获取许多html表单 [英] How I can get many html forms from a single page, in one flask function

查看:63
本文介绍了如何在一个Flask函数中从单个页面获取许多html表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@app.route('/profile<int:id>',methods=['POST','GET'])
def profile(id):
      id13=session['id']
      id_profile=id
      search=None
      row=None
      if id13 != id_profile:
            return redirect(url_for('logout'))
      if id13 == "" :
            return redirect(url_for('logout'))

      if request.method=="POST":
            search2=request.form['search']        
            sql="SELECT * FROM muzeu WHERE name LIKE '%"+search2+"%' "
            con.execute(sql)
            search=con.fetchall()
      if request.method=="POST":
            insert1=request.form["insert1"]
            insert2=request.form["insert2"]
            sql2="INSERT INTO comments VALUES('','"+insert1+"','"+insert2+"')"
            con.execute(sql2)

      sql3="SELECT * FROM comments "
      con.execute(sql3)
      row=con.fetchall()  
      return render_template("profile.html",search=search,row=row)           


and my html page forms

<form method="POST" action="">
  <div class="input-group">
    <input type="text" class="form-control" name="search" placeholder="Search">
    <div class="input-group-btn">
      <button class="btn btn-default" type="submit" name="submit1">
        <i class="glyphicon glyphicon-search"></i>
      </button>
    </div>
  </div>
</form>


<form method="POST">
<div class="input-group">
    <span class="input-group-addon">Nume</span>
    <input id="msg" type="text" class="form-control" name="insert1" placeholder="Additional Info">
  </div>
  <div class="input-group">
    <span class="input-group-addon">Descriere</span>
    <input id="msg" type="text" class="form-control" name="insert2" placeholder="Additional Info">
  </div>
 <input type="submit" name="submit2" class="btn btn-primary btn-md">
</form>

我尝试学习烧瓶,但是我对这个问题一无所知.
我尝试通过搜索,插入并支付插入内容来创建个人资料页面.

I try to learn flask and I can't get the answear on this question .
I try to make an profile page with a search ,an insert and dispaying the insert.

提交后,我收到http错误400错误的请求
浏览器(或代理)发送了该服务器无法理解的请求."
有帮助吗?

After submition i get http error 400 bad request
"The browser (or proxy) sent a request that this server could not understand."
Any help ?

推荐答案

浏览器只能提交一种表单.您正在尝试处理来自这两者的数据,但是未提交的数据在request.form中将不存在,并且会引发400错误.

Only one form can be submitted by the browser. You're trying to process the data from both, but the non-submitted data won't exist in request.form and will raise a 400 error.

您需要能够区分所提交的表单.将名称和值添加到提交"按钮,然后检查返回了哪个值以了解要执行的处理.通过添加带有名称的按钮,您处在正确的轨道上,但是您对它们的不一致,也没有在Flask中检查它们的值.

You need to be able to distinguish which form was submitted. Add a name and value to the submit button and check which value was returned to know what processing to do. You were on the right track by adding buttons with names, but you weren't consistent about them and weren't checking their value in Flask.

<!-- in the search form -->
<button type=submit name=action value=search>

<!-- in the second form -->
<button type=submit name=action value=comment>

if request.method == 'POST':
    if request.form['action'] == 'search':
        # do search action
    elif request.form['action'] == 'comment':
        # do comment action


在这种情况下,让不同的视图处理搜索和注释更有意义.创建两个单独的视图,然后将表单指向正确的URL.


In this case, it makes more sense to have different views handling searching and commenting. Create two separate views, and point the forms at the correct URLs.

<form method=post action="{{ url_for('search') }}">

<form method=post action="{{ url_for('comment') }}">

这篇关于如何在一个Flask函数中从单个页面获取许多html表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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