gpt4 book ai didi

Python/Flask/MongoDB - 错误消息 : BadRequestKeyError: 400 - Get list

转载 作者:行者123 更新时间:2023-12-01 08:11:07 25 4
gpt4 key购买 nike

我是新手,如果我没有正确解释自己,请原谅我。我正在为一个项目创建一个在线商店。我想要一个用户可以从中选择的多重选择列表。我使用 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']

最佳答案

尝试将其作为您的 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>

关于Python/Flask/MongoDB - 错误消息 : BadRequestKeyError: 400 - Get list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55246987/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com