【问题标题】:Why BadrequestKeyError is showing?为什么显示 BadrequestKeyError?
【发布时间】:2019-11-10 13:10:41
【问题描述】:

我正在做一个小项目 - 提醒系统。我有一个表单,它接受来自用户的值并插入到数据库表中。从文本框中获取值时出现问题。下面是我的代码,我也给出了错误。

<form method="POST" action="">
<input type="hidden" name="unique" value="{{session.UID}}" disabled="true">
<button type="submit" class="btn btn-primary">Confirm</button>
</form>

这是我的模板

@app.route('/home/set_reminder',methods=['POST'])
@is_logged_in
def set_reminder():
    if request.method=='POST' and form.validate():
        uid = request.form['unique']

我在这行 uid = request.form['unique'] 中收到错误消息。不明白为什么它无法获取值。

werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
KeyError: 'unique'

这是正在发生的错误。 请帮帮我。

【问题讨论】:

  • 你为什么使用disabled?无论如何,该字段是隐藏的。

标签: python python-3.x flask jinja2


【解决方案1】:

在您的 html 中,uid 输入已禁用,因此浏览器不会在 POST 请求正文中发送 uid。这会在您尝试访问 request.form.uid 时导致错误 - 它不存在。

如果值必须由浏览器返回,您可以使用 readonly 而不是 disabled

有关BadRequestKeyError 的更多信息,请参阅this answer

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-01
    • 2023-02-22
    • 2013-07-25
    • 1970-01-01
    • 2016-02-16
    • 2012-06-29
    相关资源
    最近更新 更多