In my HTML template, when I pass it to debug mode, gets the following errors:
- Refused to apply style from its MIME type (‘text/html’) is not a supported stylesheet MIME type, and strict MIME checking is enabled.;
[MIME Error]== Refused to apply style from ‘http://localhost:63342/YouTrade1/templates/YouTrade1/{%%20static%20’StyleS.Creators.css’%20%}’ because its MIME type (‘text/html’) is not a supported stylesheet MIME type, and strict MIME checking is enabled.
- ‘{% load static %}’ shows in plain text;
Pics and css files are in YouTrade1/static/YouTrade1 Template is in YouTrade1/templates/YouTrade1
I tried several solutions for the documentation of my static files and none seem to work.
All documentation according to Django best practices.
I don’t manage to understand whether there’s something wrong with either my django project setup (which looks fine), my testing environment (I click debug/run on pyCharm IDE and they said it’s all fine on their side - same with runserver on the terminal, no issue reported) or my HTML script.
I run python3 manage.py collectstatic too and nothing changes. Same with clear cache command on my browser. Error is 404 - not found .css file.
SETTINGS
"""
Django settings for YouTrade1 project.
Generated by 'django-admin startproject' using Django 5.0.2.
For more information on this file, see
https://docs.djangoproject.com/en/5.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.0/ref/settings/
"""
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = xxx
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'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',
'whitenoise.middleware.WhiteNoiseMiddleware',
]
ROOT_URLCONF = 'YouTrade1.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': os.path.join(BASE_DIR, 'YouTrade1', 'templates/YouTrade1/YT_AboutUs.html',
'templates/YouTrade1/YT_Creators.html', 'templates/YouTrade1/YT_Investors.html'),
'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 = 'YouTrade1.wsgi.application'
# Database
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/5.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/5.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.0/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'templates/YouTrade1/static/YouTrade1'),]
WHITENOISE_MIMETYPES = {
'.css': 'text/css',
}
# Default primary key field type
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
URLS
from django.contrib import admin
from django.urls import path
from django.conf.urls.static import static
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from .views import home1
urlpatterns = [
path('admin/', admin.site.urls),
path('YouTrade1\YT_Creators.html', home1, name='home1'),] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
VIEWS
from django.http import HttpResponse
from django.template import loader
from django.shortcuts import render
def home1(request):
context = {"YT_Creators": "home1"}
return render(request, "YT_Creators.html", context)
HTML Page
<!DOCTYPE html>
<html lang="en">
{% load static %}
<head>
<meta charset="UTF-8"/>
<title>Creators</title>
</head>
<body>
<link rel="stylesheet" href="{% static 'StyleS.Creators.css' %} ">
</body>
</html>
CSS File
body {
background-image: url(BeYou.jpg);
}