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.
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
-
This page failed to load a stylesheet from a URL.
-
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.
–Python fix
After countless hours of late night searching, I was forced to find a fix of my own, and I’m here to share it.
Being new to web development, I did not understand everything in my settings.py, especially the static caching and configs for my project.
Error main cause:
I previously had a static folder on an upper level of the static folder I actually intended to us, a silly mistake that I didn’t think would affect anything, so later on I simply deleted that upper level static folder, rendering it as a useless additional folder that I don’t need. That’s when some css files started return a Not Found HTML response.
Structure:
webapp
clientapp
static / This is the one I wanted (or thought) that my program was using.
webproj
static / This is what caused the error after deleting it.
So, running:
python manage.py findstatic clientapp/static/cssfolder/file.css --verbosity 2
gave me:
?: (staticfiles.W004) The directory 'project\webproj\static' in the STATICFILES_DIRS setting does not exist.
Found 'select2/select2.min.js' here:
project\webproj\clientapp\static\select2\select2.min.js
Looking in the following locations:
project\webproj\static
project\venv\Lib\site-packages\django\contrib\admin\static
project\webproj\clientapp\static
project\venv\Lib\site-packages\django_select2\static
So I commented out STATICFILES_DIRS = [BASE_DIR / 'static']
And made sure DJANGO_DEBUG=True
And deleted staticfiles folder cause I ran collectstatic before. / Also a silly mistake for local dev
Please note that I’m still new to the field having switched careers recently and have about a year of work experience so far, I successfully hosted my first Python web app a few days ago and still got so much to learn. Having that said, this fixed the error that gave me headaches for the past three days, and I hope it helps you get to it faster.
Welcome @HamdiAkoub !
We try to avoid suggesting that people do the wrong thing here.
This is what you do not want to do when you’re deploying a Django project.
Quoting directly from the docs at Settings | Django documentation | Django
Never deploy a site into production with DEBUG turned on.
Yes, getting static (and media) files served correctly in your production environment can be frustrating the first time. But you really want to take the time to understand the STATIC-related settings and understand how they work with your web server to “do the right thing”.