我已经为我的生日提醒应用程序创建了一个编辑视图,并且以前工作过,每当我链接到该编辑视图时,都会收到一个关键错误。我已经检查了get_Gift函数返回的对象,并且键字段存在。另外,使用多个字段创建的HTML表单包含错误中指示的字段。
收到的 错误 是:
werkzeug.exceptions.BadRequestKeyError:400错误的请求:浏览器(或代理)发送了该服务器无法理解的请求。 KeyError:“ giftName”
我确定我确实缺少一些明显的东西,但是我目前看不到。应该呈现HTML并使用从相应的MongoDB文档中检索到的值填充。 任何帮助将不胜感激。
Python脚本
def get_gift(gid):
query = {"giftId":gid}
gift = Gift.viewGift(query)
if gift is None:
abort(404, "Gift id {0} doesn't exist.".format(gid))
return gift
@ct.route('/<giftId>/editGifts', methods=('GET','POST'))
def editGifts(giftId):
gft = get_gift(giftId)
if request.method == 'POST':
giftId = giftId
giftName = request.form['giftName']
giftDate = datetime.strptime(request.form['giftDate'],'%Y-%m-%d')
giftDesc = request.form['giftDesc']
giftType = request.form['giftType']
giftCategory = request.form['giftCategory']
giftSentiment = request.form['giftSentiment']
giftRelInterest = list(str(request.form['giftRelInterest']).split(','))
error = None
gft = Gift(giftName,giftDate,giftDesc,giftType,giftCategory,giftSentiment,giftRelInterest,giftId)
if giftName is None:
error = 'Please provide a gift name'
if giftDate is None:
error = 'Please enter the date of gift purchase'
if giftType is None:
error = 'Please select a gift type'
if giftCategory is None:
error = 'Please choose a gift category'
if error is not None:
flash(error)
else:
query = {"giftId":giftId}
giftData = {"$set": {
"giftName": giftName,
"giftDate": giftDate,
"giftDesc": giftDesc,
"giftType": giftType,
"giftCategory": giftCategory,
"giftSentiment": giftSentiment,
"giftRelInterest": giftRelInterest
}}
Gift.updateGift_one(query,giftData)
return redirect(url_for('contact.index'))
gCategories = [{'name':'Books'},{'name':'Electronics/Computers'},{'name':'Home & Garden'},{'name':'Sports'},{'name':'Travel/Hospitality'},{'name':'Film/Theatre'},{'name':''},{'name':''}]
gTypes = [{'name':'Activity/Experience'},{'name':'Alcohol/Beverage'},{'name':'Virtual'},{'name':''},{'name':''},{'name':''},{'name':''},{'name':''}]
gSentiment = [{'name':'Positive'},{'name':'Neutral'},{'name':'Negative'},{'name':'Unknown'}]
gRelInt = [{'name':'Sports'},{'name':'Reading'},{'name':'Travel'},{'name':'Cooking'},{'name':'Art'},{'name':'Gardening'},{'name':'Gym'},{'name':'Film/Theatre'},{'name':'Music'},{'name':'DIY'}]
return render_template('gifts/editGifts.html', gft=gft, gCategories=gCategories, gTypes=gTypes, gSentiment=gSentiment, gRelInt=gRelInt)
else:
return render_template('gifts/viewGifts.html')
HTML表单
{% extends 'base.html' %}
{% block header %}
<div>
<h1>{% block title %}Edit Gift{% endblock %}</h1>
</div>
{% endblock %}
{% block content %}
<div class="ui form">
<fieldset>
<form method = "post">
<p>
<label for="giftName">Gift Name *</label>
<input name="giftName" id="giftName" value="{{ request.form['giftName'] or gft['giftName'] }}" required>
</p>
<p>
<label for="giftDate">Purchase Date *</label>
<input type="date" name="giftDate" id="giftDate" value="{{ request.form['giftDate'] or gft['giftDate'] }}" required>
</p>
<p>
<label for="giftDesc">Description</label>
<textarea name="giftDesc" id="giftDesc" rows="4" cols="50" value="{{ request.form['giftDesc'] or gft['giftDesc'] }}"></textarea>
</p>
<p>
<label for="giftType">Type *</label>
<select name="giftType" id="giftType" required>
{% for gTy in gTypes %}
<option selected="" value="{{ gTy.name }}">{{ gTy.name or gft['giftType'] }}</option>
{% endfor %}
</select>
</p>
<p>
<label for="giftCategory">Category *</label>
<select name="giftCategory" id="giftCategory" required>
{% for gC in gCategories %}
<option selected="Unknown" value="{{ gC.name }}">{{ gC.name or gft['giftCategory']}}</option>
{% endfor %}
</select>
</p>
<p>
<label for="giftSentiment">Sentiment</label>
<select name="giftSentiment" id="giftSentiment">
{% for gSen in gSentiment %}
<option selected="Unknown" value="{{ gSen.name }}">{{ gSen.name or gft['giftSentiment'] }}</option>
{% endfor %}
</select>
</p>
<p>
<label for="giftRelInterest">Related Interests</label>
<select name="giftRelInterest" id="giftRelInterest" size=5 multiple>
{% for gRI in gRelInt %}
<option value="{{ gRI.name }}">{{ gRI.name or gft['giftRelInterest'] }}</option>
{% endfor %}
</select>
</p>
<hr>
<p><tr>
<td><input type="submit" value="Save"></td>
<td><input type="reset" value="Reset"></td>
<td><input type="submit" value="Cancel" onclick="history.back()"></td>
</tr>
</p>
<hr>
</fieldset>
</form>
</div>
{% endblock %}
答案 0 :(得分:0)
对我没什么可疑的...该错误与整个跟踪一起出现,应指出确切的起源。 另外,如果它实际上是GET请求-viewGifts.html似乎很奇怪,因为它没有传递任何参数。
答案 1 :(得分:0)
当您使用request.form['key']
获取表单值时,如果request.form
中不存在键,则Flask会引发werkzeug.exceptions.BadRequestKeyError
异常,就像您得到的一样:>
werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand. KeyError: 'giftName'
它只是告诉您密钥giftName
在request.form
中不存在。 就像对dict所做的一样,您可以将所有request.form[key]
替换为request.form.get('key')
,如果密钥不存在,您将获得None
作为默认值。