gpt4 book ai didi

python - 带有 Django 的 Gunicorn 给出了静态文件的问题

转载 作者:行者123 更新时间:2023-12-04 15:26:30 26 4
gpt4 key购买 nike

有一个名为 django_server 的 Django 项目。当我跑

python manage.py runserver

页面按预期显示

DRF working fine

然后,如果我跑
gunicorn django_server.wsgi:application --bind 0.0.0.0:8000

页面显示没有样式

Gunicorn Django without style

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

The resource from “http://0.0.0.0:8000/static/....css” was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff).



Guniorn DRF console errors
在执行 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')

这是文件夹结构

Folder structure

检查静态文件夹( ls -l )中的权限,它们显示为
drwxrwxr-x 4 tiago tiago 4096 jun 2 15:49 static

检查发生问题的文件中的权限并

Ubuntu file permissions

也添加到 settings.py
import mimetypes

mimetypes.add_type("text/css",".css",True)
mimetypes.add_type("text/javascript",".js",True)

但错误仍然存​​在。

最佳答案

您需要运行 python manage.py collectstatic .

在您的 settings.py 上我推荐你使用whitenoise为您的文件提供服务。

1) pip install whitenoise
2) 添加 STATICFILES_STORAGE在 settings.py 上
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
3) 添加到您的MIDDLEWARE在 settings.py 上

`MIDDLEWARE = [

'whitenoise.middleware.WhiteNoiseMiddleware',

'django.middleware.security.SecurityMiddleware',
...

]

关于python - 带有 Django 的 Gunicorn 给出了静态文件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62155878/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com