【问题标题】:Django give Error 500 for all static files like CSS and Images, when DEBUG is False当 DEBUG 为 False 时,Django 为所有静态文件(如 CSS 和图像)提供错误 500
【发布时间】:2020-08-19 23:14:36
【问题描述】:

我尝试了用户已经发布的不同解决方案,但它们对我不起作用。

settings.py 项目

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEBUG = False
ALLOWED_HOSTS = ["*"]

STATIC_URL = '/static/'
STATICFILES_DIRS=[

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

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')

我所有的 CSS 文件都在 static 文件夹内的 style 文件夹中。 所有图片都在媒体文件夹中。

浏览器控制台日志

        Refused to apply style from 'http://127.0.0.1:8000/static/styles/LandingPage_CSS.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
        icons8-user-48.png:1 
    Failed to load resource: the server responded with a status of 500 (Internal Server Error)
        Doorakart%20icon.png:1 Failed to load resource: the server responded with a status of 500 (Internal Server Error)
        apple.jpg:1 
    Failed to load resource: the server responded with a status of 500 (Internal Server Error)
        banana.jpg:1 
    Failed to load resource: the server responded with a status of 500 (Internal Server Error)
        watermelon.jpg:1 
    .
    .
    .

    Failed to load resource: the server responded with a status of 500 (Internal Server Error)
    Refused to apply style from 'http://127.0.0.1:8000/static/styles/LandingPage_CSS.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.

HTML 文件示例

{% load static %}
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <title></title>

    <link rel="stylesheet" href="{% static 'styles/LandingPage_CSS.css' %}">
</head>

   ...
      # IMAGES ARE LOADED LIKE THIS
     <img src="media/{{item.itemImage}}" alt="img" class=" card-img-top">

   ...

另外,我想禁用DEBUG,因为我想创建我的自定义 404 错误页面。 404页面也会包含静态图片和CSS,有可能吗?也请帮助我。

【问题讨论】:

  • 这是预期行为。 Django在生产中提供静态文件或媒体文件。您应该配置 nginx 等来提供文件。

标签: django django-staticfiles django-settings django-debug-toolbar django-errors


【解决方案1】:

这是预期的行为。 Django 在生产中提供静态文件或媒体文件。您应该配置 nginx 等来提供文件。

Static file development view section of the documentation中所指定:

此视图仅在 DEBUGTrue 时有效。

那是因为这个 视图非常低效,而且可能 不安全。这仅用于本地开发,应该 永远不要在生产中使用

通常您应该配置nginxapache 网络服务器来提供静态文件。这些网络服务器可能更高效,并且具有更专用的安全工具。

Django 提供了一些工具来帮助您设置静态文件,例如使用collectstatic command [Django-doc] 在单个位置收集静态文件。该文档还描述了如何制作basic configuration for apache and nginx

还有一个包whitenoise如果你真的想让Django在生产中服务静态文件,但是said in the documentation

从 Python 提供静态文件的效率不是非常低吗?

对此的简短回答是,如果您关心性能和 效率,那么您应该在 CDN 后面使用 WhiteNoise,例如 云前。如果您这样做,那么由于缓存标头 WhiteNoise 发送,绝大多数静态请求都会被服务 直接由 CDN 直接访问,无需接触您的应用程序,所以它真的 WhiteNoise 的效率并没有太大区别。

也就是说,WhiteNoise 非常高效。因为它只需要 提供一组固定的文件,它会完成查找文件和 在初始化时预先确定正确的标题。要求 然后可以只用字典查找来查找 适当的回应。此外,当与 gunicorn 一起使用时(以及大多数 其他 WSGI 服务器)将文件下推的实际业务 网络接口由内核非常高效的sendfile处理 系统调用,而不是 Python。

【讨论】:

    猜你喜欢
    • 2019-12-02
    • 2019-06-01
    • 1970-01-01
    • 2012-11-27
    • 2018-08-24
    • 2020-10-27
    • 2019-02-04
    • 2022-12-10
    相关资源
    最近更新 更多