【问题标题】:Why Django-Admin Panel did not show css and javascript in real vps?为什么 Django-Admin Panel 在真正的 vps 中没有显示 css 和 javascript?
【发布时间】:2019-08-01 02:19:13
【问题描述】:

我有 django 管理面板在 vps (Windows Server 2012) 上工作,为什么它没有显示任何 css 样式或 boostrap?它只显示 html 而没有其他内容。

【问题讨论】:

  • 如何提供静态文件? settings.py 中的 DEBUG 变量是否设置为 False?
  • 是的,调试是错误的@NihalSangeeth

标签: python django django-admin static-files


【解决方案1】:

https://docs.djangoproject.com/en/2.1/howto/static-files/

在开发过程中,如果你使用 django.contrib.staticfiles,这将 当 DEBUG 设置为 True 时由 runserver 自动完成(参见 django.contrib.staticfiles.views.serve())。

因此,当您更改 DEBUG=False 以退出开发时,Django runserver 将不再自动提供静态文件。 这是使用http://whitenoise.evans.io/en/stable/django.html的替代方法

用于在部署中提供静态文件的白噪声

1)pip install whitenoise

2) 将'whitenoise' 添加到 installed_apps

3) 将STATIC_ROOT 提供给如下文件夹:

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

4) 运行收集STATIC_ROOT目录下的静态文件

python manage.py collectstatic

5)添加STATICFILES_STORAGE(可选)

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

6) 在除'django.middleware.security.SecurityMiddleware' 之外的所有内容之前添加中间件,例如:

MIDDLEWARE_CLASSES = [
  'django.middleware.security.SecurityMiddleware',
  'whitenoise.middleware.WhiteNoiseMiddleware',
   ...
]

7)在settings.py中改为DEBUG=False

更多详情在这里http://whitenoise.evans.io/en/stable/django.html

【讨论】:

  • 请忽略第 8 步(编辑wsgi.py)。这是针对旧版本的 WhiteNoise 的,它不适用于较新的版本。
  • 感谢您的编辑。我相信您是该项目的所有者?那里做得很好。
  • @NihalSangeeth 我试过你的解决方案,但对我不起作用。
猜你喜欢
  • 2021-12-26
  • 2017-06-01
  • 2011-06-23
  • 2019-12-13
  • 2012-09-11
  • 2013-06-07
  • 2012-01-03
  • 2017-06-22
  • 2012-09-28
相关资源
最近更新 更多