当DEBUG为False时,Django为所有静态文件(例如CSS和Images)给出错误500

问题描述:

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

I've tried different solutions already posted by users, but they didn't work for me.

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文件都具有样式静态文件夹内的文件夹。
并且所有图像都在媒体文件夹中。

All my CSS files are in style folder inside the static folder. And all images are in the media folder.

浏览器控制台日志

        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,这可能吗?

Also, I want to disable DEBUG because I want to make my custom 404 Error page. 404 page will also contain static Image and CSS, is it possible? Please help me with that too.

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

This is expected behavior. Django does not serve static files or media files in production. You should configure nginx, etc. to serve files.

文档的静态文件开发视图部分


仅当 DEBUG True 时,此视图才有效。

This view will only work if DEBUG is True.

这是因为此视图效率极低,并且可能有
不安全
。这仅用于本地开发,并且
切勿在生产中使用

That’s because this view is grossly inefficient and probably insecure. This is only intended for local development, and should never be used in production.

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

Normally you should configure nginx, apache web server to serve static files. These web servers are likely more efficient, and have more dedicated tooling for security.

Django提供了一些工具来帮助您设置静态文件,例如使用 collectstatic 命令[Django-doc] 将静态文件收集到一个位置。该文档还描述了如何为基本配置 apache nginx

Django offers some tooling to help you set up static files, for example with the collectstatic command [Django-doc] to collect static files in a single location. The documentation furthermore describes how to make a basic configuration for apache and nginx.

还有一个软件包 whitenoise Django在生产环境中提供静态文件,但作为文档中所述

There is also a package whitenoise if you really want to let Django serve static files in production, but as said in the documentation:


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

对此的简短答案是,如果您关心性能和
效率,则应该在CDN后面使用WhiteNoise strong>如
CloudFront。如果您这样做的话,由于WhiteNoise发送了缓存头
,CDN将直接为
提供绝大多数静态请求,而无需触摸您的应用程序,因此
确实不会

The short answer to this is that if you care about performance and efficiency then you should be using WhiteNoise behind a CDN like CloudFront. If you’re doing that then, because of the caching headers WhiteNoise sends, the vast majority of static requests will be served directly by the CDN without touching your application, so it really doesn’t make much difference how efficient WhiteNoise is.

也就是说,WhiteNoise非常有效。因为它只需要
服务一组固定的文件,它就完成了查找文件的所有工作,并且
在初始化时预先确定了正确的标头。然后,可以为请求
提供服务,而不仅仅是通过字典查找来找到
适当的响应。另外,当与gunicorn(以及大多数
的其他WSGI服务器)一起使用时,将文件沿
网络接口下推的实际工作是由内核非常高效的 sendfile $ c处理的$ c>
系统调用,而不是通过Python。

That said, WhiteNoise is pretty efficient. Because it only has to serve a fixed set of files it does all the work of finding files and determining the correct headers upfront on initialization. Requests can then be served with little more than a dictionary lookup to find the appropriate response. Also, when used with gunicorn (and most other WSGI servers) the actual business of pushing the file down the network interface is handled by the kernel’s very efficient sendfile syscall, not by Python.