真的很难应对 flask 的这个糟糕要求。我知道通常这是由于 Flask 在表单中找不到 [key] 引起的。但是,我已经检查了我的表单和 python 代码 40 次,看不到出现这种情况的任何原因。我已经注释掉了每一行引用 request.form 的 python 代码。我已经一一完成了,但我仍然收到一个错误的请求。然而,当我注释掉所有行时,错误的请求就消失了..任何想法都会很棒..
Python 代码;
if request.method == 'POST':
form = 'Add Package Form'
clientId = request.form['id']
date = request.form['date2']
strPrice = request.form['price']
price = float(strPrice)
adultlessons = request.form['adult']
juniorlessons = request.form['junior']
shortlessons = request.form['short']
playinglessons = request.form['playing']
notes = request.form['notes']
表格..
<form action="/addpackage" method="post" class="sky-form">
<fieldset>
<section>
<label class="label">Select Package Date</label>
<label class="input">
<i class="icon-append fa fa-calendar"></i>
<input type="text" name="date2" id="date">
</label>
</section>
<div style="margin: -25px"></div>
<fieldset>
<section>
<label class="label">Price</label>
<label class="input">
<input type="text" name="price">
</label>
</section>
<section>
<label class="label">Adult Lessons</label>
<label class="input">
<input type="text" name="adult">
</label>
</section>
<section>
<label class="label">Junior Lessons</label>
<label class="input">
<input type="text" name="junior">
</label>
</section>
<section>
<label class="label">Short Game Lessons</label>
<label class="input">
<input type="text" name="short">
</label>
</section>
<section>
<label class="label">Playing Lessons</label>
<label class="input">
<input type="text" name="playing">
</label>
</section>
<section>
<label class="label">Notes</label>
<label class="textarea textarea-expandable">
<textarea rows="3" name="notes"></textarea>
</label>
<div class="note"><strong>Note:</strong> expands on focus.</div>
</section>
</fieldset>
</fieldset>
<!-- hidden client id -->
<input type="hidden" name="id" value="{{ client.id }}">
<!-- /hidden client id -->
<footer>
<button type="submit" name="addpackage" value="package" class="button">Add Package</button>
</footer>
</form>
最佳答案
这是一个半答案,但对于评论来说太长了。
如果您在 Flask 应用程序中启用调试,您应该会获得详细的回溯,准确指示问题发生的位置(在浏览器中和控制台上)。
如果您的应用程序当前有类似以下内容:
app.run()
只需将 debug
参数设置为 true
:
app.run(debug=True)
如果启用调试后您仍然不确定导致问题的原因,请更新您的问题以包含回溯。
无论如何,如果我将您的表单和代码转储到一个简单的 Flask 应用程序中,只要我提供 price
的数值,一切似乎都可以正常工作字段。
关于python - Flask 的错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40442827/