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.

I am also having the same issue, i am getting these three errors and i have tried many solutions on the internet but it did’nt resolved. Every link, directory structure added is correct, but still the issue persists. I am using Django 5.0.2
These are errors shown in console.
" 1. Verify stylesheet URLs

  1. This page failed to load a stylesheet from a URL.

  2. AFFECTED RESOURCES

1. 1 source

  1. 127.0.0.1/:17

Failed to load resource: the server responded with a status of 404 (Not Found)
127.0.0.1/:1

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


"
I will be very grateful if you can help me…

1 Like

Welcome @aimaarif !

This topic has been marked as solved. Your post is likely to get more attention if you opened a new topic for it.

When you do so, please show the view and the template being rendered here. Also post your settings.py file.

Post the text of the code into the body of your post, do not post images of code.

Side note: When posting code (or templates, error messages, tracebacks, etc), enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted. (I’ve taken the liberty of editing your original post. Please remember to do this in the future.)

1 Like

i am getting the same error but in production my local host is working fine

settings.py

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

base.html

<link href="{% static "css/custom.css" %}" type="text/css" rel="stylesheet">

other css files are working but getting error only in this

and the error is
https://legalsync.co.in/static/css/custom.css’ because its MIME type (‘text/html’) is not a supported stylesheet MIME type, and strict MIME checking is enabled.

I am facing same problem,have you solved it?

Welcome @Manika2023 !

Please see my reply at MIME type (text/html) is not supported as css - #9 by KenWhitesell

There are many different possibilities regarding the cause of this error. There’s no one-size-fits-all solution. If you’re encountering this specific error, please open a new topic for the discussion, and include the details about your situation.
(development or production? static file settings in your static.py? The template rendering the css causing the error? A description of your directory structure and the location of these static files within that structure?)

Ok sir, thank for replying
Actually, I have solved that problem by running python manage.py runserver -insecure . Actually my settings .py was configured correctly to static file. But not working, now it’s all fine after running this command

Just to be clear, if you’re doing this in your development environment, that’s ok. But if this is how you’re deploying your project to a live environment, it’s seriously wrong. What you’ve done is work around the real issue by potentially creating other problems for yourself.

Welcome @LirimIslamii !

If you have a specific issue with which you would like assistance, please create a new topic here for your issue, with as many details about it as you can provide.

I do not provide private assistance. If you have an issue needing to be resolved, I can try to help you here.

Note: I do not use IIS. I’m unable to provide any specific IIS-related assistance. Maybe someone else would be able to help you here.

As a new user, you can create a new topic.

Don’t post images of code, directories, etc.

Copy/paste the text data into the body of your post. If it is preformatted text like code, error messages, tracebacks, html, or templates, post the text between lines of three backtick - ` characters. This means you’ll have a line of ```, then the code (or html, etc), then another line of ```.

I ran into this issue in IIS - this answer was very helpful to me - essentially, FastCGI set it up as a folder, but it needed to be an application.