首页 > 解决方案 > 在 CPanel 中使用 Python 在 Django 管理面板中加载静态文件

问题描述

我已经使用 CPanel 在我的服务器中设置了一个 Django Python 应用程序。wsgi 配置正确,index.html(没有任何 css)运行正常,但管理面板没有正确加载 css。

我一直在阅读我需要正确设置静态文件路由,但这些似乎都不起作用。

我的 settings.py 文件在已安装的应用程序中有 static 指令,最后这行是我的 python virtualenv 的静态文件所在的位置。

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static') #after collect static is run all the static files will be put in this folder
STATICFILES_DIRS = (['/home/djangouser/virtualenv/api/3.5/lib/python3.5/site-packages/django/contrib/admin/static/admin/'])

我已经尝试重新启动应用程序,但这些都不起作用,我的管理页面仍然在没有 css 的情况下加载。

我的 index.html 没有任何 CSS,所以我不知道这是否只是管理员的问题......但我需要解决这个问题。

谢谢你。

标签: pythondjangocpaneldjango-staticfiles

解决方案


完美的解决方案是通过网络服务器提供静态文件。但是您也可以使用 Cling 使用 Python 来完成:

$ pip install dj-static static3

并更新您的 wsgi.py:

...
from dj_static import Cling
...
application = Cling(get_wsgi_application())

推荐阅读