MIME type (text/html) is not supported as css

Hi!

When I run python manage.py runserver in terminal, I go to my site and I see in devtools error:

Refused to apply style from ‘http://127.0.0.1:8000/csscode.css’ because its MIME type (‘text/html’) is not a supported stylesheet MIME type, and strict MIME checking is enabled.

The files are corectly linked, name in href atribute is correct. I searched all internet but I haven’t found the solution. When I open css file in browser it shows 404 error.
I’m using Google chrome, PyCharm from jet brains, and django 4.1.5.
Thanks beforehand and sorry for my bad english.

This error is almost always caused by a 404 being generated by the server. The 404 returns an html page instead of the css, causing this error.

To allow us to help you resolve this, please provide the following information:

  • Describe the directory structure for your project,
  • Tell us what directory that csscode.css file is in,
  • Show us the STATIC_URL and STATICFILES_DIRS settings from your settings.py file
  • Show us the line in your template where you’re trying to load that file

(And your English is just fine - don’t worry about it!)

1 Like

main
main
| init.py
| asgi.py
| settings.py
| urls.py
| wsgi.py
app
| migrations
| templates
| | app
| | | csscode.css
| | | htmlcode.html
| init.py
| admin.py
| apps.py
| models.py
| test.py
| urls.py
| views.py
that’s the structure

STATIC_URL = ‘/static/’
STATICFILES_DIRS is default
<link rel=“stylesheet” type=“text/css” href=“csscode.css”/>

Couple different things here.

  • CSS files are generally static files and not templates.

    • You either need to move that to your static directory
      or
    • You need to render it and return it as a css file response (highly unusual case - unless you know exactly what you’re doing here, this probably isn’t what you’re trying to do.)
  • You should be rendering a static tag url for the href.

Review the docs and examples at:

main
main
| init.py
| asgi.py
| settings.py
| urls.py
| wsgi.py
app
| migrations
| templates
| | app
| | | htmlcode.html
| init.py
| admin.py
| apps.py
| models.py
| test.py
| urls.py
| views.py
|staticfiles
||csscode.css
|static
so now this is how structure of my project looks
and in href atribute I’ve written:
“{% static ‘C:/Users/Admin/PycharmProjects/site/base/staticfiles/csscode.css’%}”
but, now server throws:
net::ERR_ABORTED 500 (Internal Server Error)

Notice that in the examples in the referenced docs that they don’t specify the complete path to the file. You only specify a path relative to an entry in the STATICFILES_DIRS setting.

I had this issue for three days, I’ve searched all internet and didn’t found anything but you helped me. Massive thanks.