Flaic-Mobility与Gunicorn失败

时间:2017-08-06 12:39:22

标签: python flask gunicorn

我正在使用Flask-Mobility根据浏览器呈现不同的模板。

我的本​​地计算机上的一切正常,但在服务器上,我得到AttributeError: 'Request' object has no attribute 'MOBILE'我正在使用gunicorn

当我通过仅将其作为python实例运行来测试应用程序时,没有gunicorn和没有工人等,它可以正常工作。我看到Flask-Mobility做了@app.before_request我猜我在使用Gunicorn时没有处理这个问题。

失败的一行是:

if request.MOBILE == True:

以下是完整的追溯:

ERROR in app: Exception on /content/3/intro-to-genetics-1/1/baby-ball [GET]
Traceback (most recent call last):
  File "virtualenv/lib/python3.5/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "virtualenv/lib/python3.5/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "virtualenv/lib/python3.5/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "virtualenv/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "virtualenv/lib/python3.5/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "virtualenv/lib/python3.5/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "virtualenv/lib/python3.5/site-packages/flask_mobility/decorators.py", line 35, in wrapper
     return f(*args, **kwargs)
File "app/main/views.py", line 333, in direct_screen_nav
    if request.MOBILE == True:
File "virtualenv/lib/python3.5/site-packages/werkzeug/local.py", line 347, in __getattr__
    return getattr(self._get_current_object(), name)
AttributeError: 'Request' object has no attribute 'MOBILE'

1 个答案:

答案 0 :(得分:0)

我弄清楚出了什么问题。问题不在于枪炮,而在于使用蓝图。我修改了Flask-Mobility的{​​{1}} Mobility函数,将任何蓝图作为可选参数,并将其添加到__init__

def init_app()

因此,我不是将库初始化为if blueprint is not None: @blueprint.before_app_request def before_app_request(): ctx = stack.top if ctx is not None and hasattr(ctx, 'request'): self.process_request(ctx.request) ,而是使用Mobility(app)进行初始化,一切正常。不完全确定为什么它之前没有枪支的工作,但是,可能是因为我在两个不同的背景下使用相同的名称Mobility(app, blueprint)并且偶然地,它起作用。