28

I have installed virtualenv in my localhost to run a django app with 1.8 version but when running it the css and js files doesn't load.

I get

Resource interpreted as Stylesheet but transferred with MIME type application/x-css

I have tried some options but they don't solve the issue either. I am running the same configuration on other PC and it works.

My HTML loads the css with :

<link href="/static/css/bootstrap.css" rel="stylesheet" type="text/css">
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
jesicadev18
  • 2,942
  • 5
  • 22
  • 39

10 Answers10

31

Adding following snippet into settings.py file may fix your problem:

import mimetypes
mimetypes.add_type("text/css", ".css", True)
Andriy Ivaneyko
  • 20,639
  • 6
  • 60
  • 82
  • Thanks, that worked ! but why I am having this issue only on some PCs ? – jesicadev18 Feb 22 '16 at 15:34
  • 1
    Because that's related to incorrect association of css file on OS base, after adding lines in answer above all css files should be associated correctly. Also you can take a look on accepted answer of source i've pointed to. Have a nice day )! – Andriy Ivaneyko Feb 22 '16 at 15:51
  • Man, this was driving me crazy. The app worked fine on my Windows 10 laptop, but did not serve the status assets correctly on my desktop at work. This fixed the problem!! – MattSlay Nov 14 '19 at 01:07
  • This didn't help me at all, I use Windows 7. Running it under virtual environment. – KurtNovice Nov 28 '19 at 07:18
  • he path is correct and verified with Pycharm. It was placed in settings.pu `import mimetypes mimetypes.add_type ("text / css", ".css", true) STATIC_URL = '/ static /'` But it still doesn't load the link. – Fernando López Sep 25 '20 at 22:22
21

This particular behaviour varies between the development (DEBUG=True) and deployment environment (DEBUG=False).

So if you are developing locally with DEBUG=False there is a high chance of this error. But once deployed on any server it will work without any error. If you want to avoid this error during development set DEBUG=True.

Paolo
  • 20,112
  • 21
  • 72
  • 113
M A Rahul
  • 239
  • 2
  • 3
  • How can one avoid this error during production with DEBUG=False? – Adam P Nov 16 '22 at 05:41
  • I did set up the debug mode on True, but still no success. I have developed [my question there](https://stackoverflow.com/questions/75520201/flask-and-css-file-in-pandoc-generated-html). Thanks for your help! – laconis Feb 25 '23 at 16:12
17

"whitenoise" will solve "MIME type" error then CSS is loaded successfully:

First, install "whitenoise":

pip install whitenoise

Then, set it to "MIDDLEWARE" in "settings.py". Finally, CSS is loaded successfully:

MIDDLEWARE = [
    # ...
    "django.middleware.security.SecurityMiddleware",
    "whitenoise.middleware.WhiteNoiseMiddleware", # Here
    # ...
]
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
  • My problem was in production, trying to host Django app on a shared hosting, whitenoise did the magic. – oriohac May 15 '23 at 20:06
12

I ran into this issue during development (production was using Nginx and serving from /static_cdn folder without any issues).

The solution came from the Django docs: https://docs.djangoproject.com/en/3.1/howto/static-files/#serving-static-files-during-development

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Glen Carpenter
  • 392
  • 2
  • 9
5

open your Chrome by F12 Developer Tool and check what you actually received. In my case, the CSS file actually redirected to another page. so MIME is text/html not text/css

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Jess Chen
  • 3,136
  • 1
  • 26
  • 35
2

Access the CSS file directly. If you get a 404 error that is your answer, because Django serve you a text/html file when the browser expect text/css.

Try to fix your static files, and the problem is most likely solved.

enter image description here

Punnerud
  • 7,195
  • 2
  • 54
  • 44
1

If you're using Centos and having similar issues (mine were with svgs) then you might need to install the mailcap package if it doesn't exist (as per this answer).

Marthinwurer
  • 116
  • 1
  • 6
1

If you happen to be using the Django whitenoise plugin, then the mimetypes module is not used, and you need to pass in a dictionary of custom types in settings.py:

WHITENOISE_MIMETYPES = {
    '.xsl': 'application/xml'
}
shuckc
  • 2,766
  • 1
  • 22
  • 17
0

In my case I only loaded

{% load static %}

in django and not added the

'{%static '<filename>'%}' 

in hrefs when I added this the error's gone

-3

Set DEBUG = TRUE, if you are in dev. You can later in production set it false.

vimuth
  • 5,064
  • 33
  • 79
  • 116
ብሩክ
  • 23
  • 3
  • 6