python-烧瓶html形式help/suggestions

python-flask html form help/suggestions

我正在呈现一个页面供用户浏览练习网站上的广告列表。在 'browse' 页面上,我有三个单独的表单,两个是 select 字段,另一个是文本字段。 select 字段允许用户更改位置或交易类型,文本字段允许他们更改他们正在搜索的内容。我想做这个纯粹的 HTML 而不是使用 flask-wtf。我已经成功地让他们可以更改位置的部分正常工作,但是一旦我为其他选项添加代码,它就会中断并且我收到 400 错误请求错误。关于为什么在我添加更改项目和交易类型的表格时这不起作用的任何建议或指导?

 @app.route('/find-it/', methods=['GET','POST'])
def browse():
   try:

    location = session.get('location', None)
    transType = session.get('transType', None)
    item = session.get('item', None)        

    data = browseQuery()

     if request.method == 'POST':
        if request.form['changeLocation'] != '':
            print('location changed')
            location = request.form['changeLocation']
            session['location'] = location
            return redirect(url_for('browse', location=location))
        elif request.form['changeType'] != '':
            print('type changed')
            transType = request.form['changetype']
            session['transType'] = transType
            return redirect('browse', transType=transType)
        else:
            if request.form['changeItem'] != '':
                print('item changed')
                item = request.form['changeItem']
                session['item'] = item
                return redirect(url_for('browse', item=item))

      return render_template('all-classifieds.html',location=location,transType=transType, data=data)
except Exception as e:
    return (str(e))

HTML:

<form method="post" id="changeLocation" name="changeLocation" action="">

                    <select name="changeLocation" id="changeLocation" style="margin-right: 5%; float: left;">
                        <optgroup label="Where?">
                            <option selected style="display:none;color:#eee;"></option>
                            <option>option 1</option>
                            <option>option 2</option>

                        </optgroup></select></form>


<button type="submit" id="submit" form="changeLocation" style="padding: 0 3px; float:left;"
                        class="btn btn-info btn-sm">
                    <i class="glyphicon glyphicon-search"></i>
                </button>




 <form method="post" name="changeType" id="changeType">


                    <select name="changeType" id="changeType" style="margin-right: 5%; float: left;">
                        <optgroup label="...">
                            <option selected style="display:none;color:#eee;"></option>
                            <option>option 1</option>
                            <option>option 2</option>
                            <option>option 3</option>

                        </optgroup>
                    </select>



<form method="post" name="changeType" id="changeType">


                    <select name="changeType" id="changeType" style="margin-right: 5%; float: left;">
                        <optgroup label="Look for things that are...">
                            <option selected style="display:none;color:#eee;"></option>
                            <option>asdf</option>
                            <option>asdfasdf</option>
                            <option>asdfasdfasdf</option>

                        </optgroup>
                    </select></form>

<button type="submit" id="submit" form="changeType" style="padding: 0 3px; float:left;"
                        class="btn btn-info btn-sm">
                    <i class="glyphicon glyphicon-search"></i>
                </button>


<form method="post" name="changeItem" id="changeItem">
                    <input type="text" name="changeItem" value="" id="changeItem" placeholder=""/>
                </form>

<button type="submit" id="submit" form="changeItem" style="padding: 0 3px; float:left;"
                        class="btn btn-info btn-sm">
                    <i class="glyphicon glyphicon-search"></i>
                </button>

您询问的问题:

if request.form['changeLocation'] != '':

您可能正在尝试查明是否使用了该特定表格。问题是:当使用正确的形式时,此检查有效,但如果使用不同的形式,request.form 字典会抛出关键错误。但这不是正常的KeyError。 Flask 会将 "special" 错误转换为 400 Bad Request.

要检查表单数据中是否存在某个键,请使用 in 运算符

if "changeLocation" in request.form:
    # you can use request.form["changeLocation"] here
elif "changeType" in request.form:
    # here request.form["changeType"] is present
...

另一个问题:

这一行

render_template('all-classifieds.html',location=location,transType=transType, data=data)

不起作用,因为 locationtransType 是在 if 块中定义的。如果不执行块,它将失败。

有些不同:

else:
    if condition:
         do_something

可以改写为

elif condition:
    do_something