【发布时间】:2020-11-20 21:11:19
【问题描述】:
今天我的 Django 项目 CSS 文件出现问题。当我尝试访问我的页面时,我的 CSS 文件没有加载。起初,我以为问题只出在 Django 管理员身上,所以我问了this question,然而,我认为这个问题值得单独提问。
我的页面正常加载,只是没有 CSS 样式,也没有出现错误。但是,在 chrome 控制台中,我发现了这个错误:
Refused to apply style from 'https://[mywebsite].com/static/pathToMyCssFile/file.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
我不明白"its MIME type ('text/html') is not a supported stylesheet MIME type" 是什么意思?它是将我的 CSS 代码检测为 HTML 还是相反?
我的settings.py:
"""
Django settings for mysite project.
Generated by 'django-admin startproject' using Django 3.0.8.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""
import os
import mimetypes
# A Bug is was encountering
mimetypes.add_type("text/css", ".css", True)
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '#######################################' # Im supposed to keep this secret?
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
SECURE_SSL_REDIRECT = True
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'students_app.apps.StudentsAppConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'mysite.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'mysite.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
# I ran python manage.py collectstatic. Still fails to work
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = '/static/'
我也试过this解决方案。
编辑:我的管理 CSS 文件返回 404
Admin Base.CSS
【问题讨论】:
-
尝试打开
file.css。如果是django 404错误页面,给出这个页面的截图 -
@Talha Quddoos 由于 GAEfan 的回答,当我打开我的应用程序静态文件时不再出现错误。但是,我的管理页面 css 文件有 404 错误