WTForms raises "TypeError: 'unicode' does not have the buffer interface" on Heroku

WTForms raises "TypeError: 'unicode' does not have the buffer interface" on Heroku

当我 运行 我在 Heroku 上的应用程序并提交 Flask-WTF 表单时,我得到以下回溯错误 TypeError: 'unicode' does not have the buffer interface。 运行 该应用程序在本地运行。从回溯来看,Flask-WTF 似乎在验证 CSRF 令牌时出现了问题。为什么会出现此错误以及如何解决?

Exception on /restaurants/1/menu/add/ [POST]
Traceback (most recent call last):
  File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/app/restaurantApp/views.py", line 95, in newMenuItem
    if form.validate_on_submit():
  File "/app/.heroku/python/lib/python2.7/site-packages/flask_wtf/form.py", line 166, in validate_on_submit
    return self.is_submitted() and self.validate()
  File "/app/.heroku/python/lib/python2.7/site-packages/wtforms/form.py", line 310, in validate
    return super(Form, self).validate(extra)
  File "/app/.heroku/python/lib/python2.7/site-packages/wtforms/form.py", line 152, in validate
    if not field.validate(self, extra):
  File "/app/.heroku/python/lib/python2.7/site-packages/wtforms/fields/core.py", line 204, in validate
    stop_validation = self._run_validation_chain(form, chain)
  File "/app/.heroku/python/lib/python2.7/site-packages/wtforms/fields/core.py", line 224, in _run_validation_chain
    validator(form, self)
  File "/app/.heroku/python/lib/python2.7/site-packages/flask_wtf/form.py", line 109, in validate_csrf_token
    if not validate_csrf(field.data, self.SECRET_KEY, self.TIME_LIMIT):
  File "/app/.heroku/python/lib/python2.7/site-packages/flask_wtf/csrf.py", line 111, in validate_csrf
    return safe_str_cmp(hmac_compare, hmac_csrf)
  File "/app/.heroku/python/lib/python2.7/site-packages/werkzeug/security.py", line 117, in safe_str_cmp
    return _builtin_safe_str_cmp(a, b)
TypeError: 'unicode' does not have the buffer interface
<form method="POST">
    {{ form.hidden_tag() }}
    <input type="submit" value="Add">
</form>
@app.route("/restaurants/<int:restaurant_id>/menu/add/", methods=["GET", "POST"])
def newMenuItem(restaurant_id):
    form = menuItemForm()

    if form.validate_on_submit():
        flash("new item added")
        return redirect(url_for("restaurantMenu"))
    else:
        return render_template("newMenuItem.html", form=form)

您的 Werkzeug 版本很旧。请参阅 this bug report,并在 2014 年发布了修复程序。Werkzeug 在 Flask-WTF 用于 CSRF 的方法中比较了错误的数据。升级您的 Werkzueg 版本并将其记录在您的需求文件中。今天的当前版本是 0.11.4.

pip install -U Werkzeug
Werkzeug==0.11.4

鉴于您使用的是非常过时的 Werkzeug 版本,您应该确保您的其他库也是最新的。