2016-03-05 44 views
2

當我在Heroku上運行我的應用程序並提交一個Flask-WTF表單時,出現以下跟蹤錯誤TypeError: 'unicode' does not have the buffer interface。在本地運行應用程序。從回溯看來,Flask-WTF驗證CSRF令牌看起來像是一個問題。爲什麼我得到這個錯誤,我該如何解決它?WTForms在Heroku上引發「TypeError:'unicode'沒有緩衝區接口」

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) 
+0

你在本地使用Python 2.7嗎? – Chris

+0

是,2.7.6。 – setagana

+0

@davidism補充,讓我知道如果還有什麼缺失。 – setagana

回答

2

你有一個很老的版本WERKZEUG的。請參見this bug report,2014年發佈修復程序.Werkzeug正在比較Flask-WTF用於CSRF的方法中的錯誤數據。升級您的Werkzueg版本並將其記錄在您的需求文件中。當前版本是0.11.4。

pip install -U Werkzeug 
Werkzeug==0.11.4 

既然你使用WERKZEUG非常過時的版本,你應該確保你的其他庫是最新的爲好。

+0

謝謝@davidism,我正在從一個udacity課程的流浪文件中工作,它完全有可能列出一些較老的庫。更新我的Heroku VS上的Werkzeug已經解決了這個問題。 – setagana