我是这个新手,所以如果我没有正确解释自己,请原谅我。我正在为项目创建在线商店。我想要一个可供用户选择的多重选择列表。我正在使用Python,Flask和MongoDB / mlab来存储和检索数据。到目前为止,它看起来像这样:
@app.route('/update_item/<item_id>', methods=['POST'])
def update_item(item_id):
store = mongo.db.store
store.update( {'_id': ObjectId(item_id)},
{
'name':request.form['name'],
'type':request.form['type'],
'color':request.form['color'],
'material':request.form['material']
})
return redirect(url_for('main_store'))
看起来像这样的HTML:
<form action="{{ url_for('update_item', item_id=item._id) }}" method="POST" class="col s12">
<select id="material" name="material" value="{{store.material}}">
<option value="" disabled selected>Select</option>
<option value="denim" name="denim">Denim</option>
<option value="cotton" name="cotton">Cotton</option>
<option value="viscose" name="viscose">Viscose</option>
<option value="silk" name="silk">Silk</option>
</select>
</form>
但是我一直收到此错误消息
BadRequestKeyError: 400 Bad Request: KeyError: 'material'
Traceback (most recent call last)
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 2309,
in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 2295,
in wsgi_app
response = self.handle_exception(e)
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1741,
in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 2292,
in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1815,
in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1718,
in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1813,
in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1799,
in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/ubuntu/workspace/app.py", line 74, in update_item
'material':request.form['material']
File "/usr/local/lib/python3.4/dist-
packages/werkzeug/datastructures.py", line 431, in __getitem__
raise exceptions.BadRequestKeyError(key)
BadRequestKeyError: 400 Bad Request: KeyError: 'material'
我在做什么错? “名称”,“类型”和“颜色”本身就可以正常工作。我尝试了以下方法,但这也没有解决。谢谢。
'material':request.form.getlist['material']
答案 0 :(得分:1)
尝试将此作为您的html
<select id="material" name="material" placeholder="Select">
<option value="denim">Denim</option>
<option value="cotton">Cotton</option>
<option value="viscose">Viscose</option>
<option value="silk">Silk</option>
</select>