Django,gunicorn,nginx 抛出 504 Gateway Time-out: AttributeError: module 'static' has no attribute 'Cli
Posted
技术标签:
【中文标题】Django,gunicorn,nginx 抛出 504 Gateway Time-out: AttributeError: module \'static\' has no attribute \'Cling\'【英文标题】:Django, gunicorn, nginx throws 504 Gateway Time-out: AttributeError: module 'static' has no attribute 'Cling'Django,gunicorn,nginx 抛出 504 Gateway Time-out: AttributeError: module 'static' has no attribute 'Cling' 【发布时间】:2019-07-10 13:51:01 【问题描述】:昨天我在线部署了我的小网站的新版本,并立即收到 504 Gateway Time-out 错误。
在对日志文件进行了一些挖掘之后,我发现了以下错误:
File "/home/dev/venuepark/ENV/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/home/dev/venuepark/ENV/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 137, in inner_run
handler = self.get_handler(*args, **options)
File "/home/dev/venuepark/ENV/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/runserver.py", line 27, in get_handler
handler = super().get_handler(*args, **options)
File "/home/dev/venuepark/ENV/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 64, in get_handler
return get_internal_wsgi_application()
File "/home/dev/venuepark/ENV/lib/python3.6/site-packages/django/core/servers/basehttp.py", line 44, in get_internal_wsgi_application
return import_string(app_path)
File "/home/dev/venuepark/ENV/lib/python3.6/site-packages/django/utils/module_loading.py", line 17, in import_string
module = import_module(module_path)
File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/dev/venuepark/venuepark/venuepark/wsgi.py", line 17, in <module>
application = Cling(MediaCling(get_wsgi_application()))
File "/home/dev/venuepark/ENV/lib/python3.6/site-packages/dj_static.py", line 97, in __init__
super(MediaCling, self).__init__(application, base_dir=base_dir)
File "/home/dev/venuepark/ENV/lib/python3.6/site-packages/dj_static.py", line 48, in __init__
self.cling = static.Cling(base_dir)
AttributeError: module 'static' has no attribute 'Cling'
我的 wsgi.py:
import os
from django.core.wsgi import get_wsgi_application
from dj_static import Cling, MediaCling
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'venuepark.config.settings.production_settings')
application = Cling(MediaCling(get_wsgi_application()))
但 dj-static 已正确安装。
什么可能导致这个问题? 提前致谢
编辑:
当我在本地运行生产设置时,一切正常。但是,当我尝试在生产服务器上运行它们时(不是使用 gunicorn,而是使用 django 自己的运行服务器),我得到的错误与我在 gunicorn 错误日志中收到的错误相同(请参阅上面的错误)。
我也运行本地设置,它们也会引发上述错误。
编辑 2:
我已经创建了一个新环境并重新安装了需求文件,但错误仍然存在。 AttributeError: module 'static' has no attribute 'Cling'
【问题讨论】:
嗯。您的生产服务器上的venv
看起来有问题。也许你没有激活它?您可以重新创建 virtualenv 并重试吗?
嘿@AlexYu,其实我刚刚做了,错误还是一样
再试一次,我发布了我的答案。你甚至可以做单行:mkdir /tmp/djstatic && cd /tmp/djstatic && pipenv install django dj_static && pipenv run python -c "from dj_static import Cling"
【参考方案1】:
再次检查您的环境创建:
mkdir /tmp/djstatic && cd /tmp/djstatic
pipenv install django dj_static
pipenv run python -c "from dj_static import Cling"
它必须有效。
我假设您使用pipenv
来创建项目环境。
【讨论】:
我没有,我在 venv 中使用 python 构建 但是。我在环境中打开python运行from dj_static import Cling
,没有报错。所以现在我很困惑为什么会出现这个错误
掌握venv
和pip
是python程序员必备的技能。但是pipenv
是两者的结合——我建议尽可能使用它以上是关于Django,gunicorn,nginx 抛出 504 Gateway Time-out: AttributeError: module 'static' has no attribute 'Cli的主要内容,如果未能解决你的问题,请参考以下文章
django-gunicorn-nginx:502 网关错误
Django-Gunicorn-Nginx 部署没有通过 Nginx
Django、Nginx、Gunicorn 和 AngularJS 应用程序结构