2

In my Django project I added a new 404 page, that gets called when I enter an invalid url. But, the CSS file is not loaded with page, so it appears mis-formatted.

The browser gives me this error: "Refused to apply style from because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled."

The point is, There are other CSS files in the 'static' folder that are loaded correctly without this error, like the bootstrap.css file...

My settings.py

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

ALLOWED_HOSTS = ['localhost', '127.0.0.1']

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'collected_static')

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

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]

My urls.py

urlpatterns = [
    ...
]


handler400 = 'utils.views.error_400'
handler403 = 'utils.views.error_403'
handler404 = 'utils.views.error_404'
handler500 = 'utils.views.error_500'

My utils.views.py

def error_400(request, exception):
        data = {}
        return render(request, 'utils/mv_admin_400.html', data)

def error_403(request,  exception):
        data = {}
        return render(request, 'utils/mv_admin_403.html', data)

def error_404(request,  exception):
        data = {}
        return render(request, 'utils/mv_admin_404.html', data)

def error_500(request):
        data = {}
        return render(request, 'utils/mv_admin_500.html', data)

My 404.html

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link href="{% static 'css/handlers/error-nf.css' %}" rel="stylesheet" type="text/css" />
  </head>

My Folder structure

src
  - mysite
  - static
    -- css
      --- bootstrap.css
      --- handlers
        ---- error-nf.css

Also I tried to move the css file out of the "handlers" folder to the parent "css" folder, but error still occur.

Abuzaid
  • 853
  • 8
  • 28
  • Does this answer your question? [Refused to apply style from... because its MIME type ('text/html') is not a supported style-sheet MIME type, and strict MIME checking is enabled](https://stackoverflow.com/questions/48904452/refused-to-apply-style-from-because-its-mime-type-text-html-is-not-a-supp) – bmons Apr 21 '20 at 14:20
  • @bmons Unfortunately, No... – Abuzaid Apr 21 '20 at 14:25
  • I assume your 404 html page is returned on this css file url request (that's why it says about wrong mime type). So, if other css files work fine, check this file location, spelling or run `collectstatic` if you did previously. – Ivan Starostin Apr 21 '20 at 14:37
  • @IvanStarostin.. path is correct.. also I can see the css file in the "collected_static" folder with the correct path... But the HTML can't load it – Abuzaid Apr 21 '20 at 14:50

0 Answers0