【发布时间】: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