WTForms引发" TypeError:' unicode'没有缓冲接口"在Heroku上

时间:2016-03-05 15:47:20

标签: python heroku flask wtforms flask-wtforms

当我在Heroku上运行我的应用程序并提交Flask-WTF表单时,我得到以下错误^的回溯。在本地运行应用程序。从回溯看起来,Flask-WTF验证CSRF令牌是个问题。为什么我会收到此错误以及如何解决?

$
TypeError: 'unicode' does not have the buffer interface
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

1 个答案:

答案 0 :(得分:2)

你有一个非常古老的Werkzeug版本。请参阅this bug report,其中包含2014年发布的修补程序.Werkzeug正在比较Flask-WTF用于CSRF的方法中的错误数据。升级您的Werkzueg版本并将其记录在您的需求文件中。今天的当前版本是0.11.4。

pip install -U Werkzeug
Werkzeug==0.11.4

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