带有 Django 的 Gunicorn 给静态文件带来了问题

得到一个名为 django_server 的 Django 项目。当我跑步时

python manage.py runserver

该页面按预期显示

http://img4.mukewang.com/63a112cd00017c9d07200445.jpg

然后,如果我跑

gunicorn django_server.wsgi:application --bind 0.0.0.0:8000

页面显示没有样式

http://img.mukewang.com/63a112da0001827e05070540.jpg

检查控制台,可以看到 .css 和 .js 文件都出现以下错误

来自“ http://0.0.0.0:8000/static/....css ”的资源由于 MIME 类型(“text/html”)不匹配(X-Content-Type-Options:nosniff)而被阻止。

http://img2.mukewang.com/63a112e700014dae18450485.jpg

在执行 gunicorn 命令的终端中,可以读取


NOT FOUND: /static/rest_framework/css/bootstrap.min.css

NOT FOUND: /static/rest_framework/css/bootstrap-tweaks.min.css

...

在 settings.py 我提到


BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

STATIC_URL = '/static/'

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


梦里花落0921
浏览 123回答 1
1回答

慕盖茨4494581

你需要运行python manage.py collectstatic。在你settings.py身上,我建议你使用whitenoise来提供你的文件。1) pip install whitenoise2)添加STATICFILES_STORAGEsettings.pySTATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'3)添加到你MIDDLEWARE的 settings.py`MIDDLEWARE = [  'whitenoise.middleware.WhiteNoiseMiddleware',  'django.middleware.security.SecurityMiddleware',  ...]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python