35👍
Adding following snippet into settings.py
file may fix your problem:
import mimetypes
mimetypes.add_type("text/css", ".css", True)
23👍
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
.
- [Django]-Django error: relation "users_user" does not exist
- [Django]-Django: Error: You don't have permission to access that port
- [Django]-How to do SELECT MAX in Django?
18👍
"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
# ...
]
- [Django]-Django – How to increment integer field from user input?
- [Django]-Automatic creation date for Django model form objects
- [Django]-Creating multiple objects with one request in Django and Django Rest Framework
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)
- [Django]-Django model blank=False does not work?
- [Django]-How to debug in Django, the good way?
- [Django]-Django – Create A Zip of Multiple Files and Make It Downloadable
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
- [Django]-Managing static files for multiple apps in Django
- [Django]-How to catch the MultipleObjectsReturned error in django
- [Django]-In a Django form, how do I make a field readonly (or disabled) so that it cannot be edited?
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.
- [Django]-Django Password Generator
- [Django]-Django template if or statement
- [Django]-Split models.py into several files
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).
- [Django]-Django celery task: Newly created model DoesNotExist
- [Django]-Django unit testing with date/time-based objects
- [Django]-Django Pass Multiple Models to one Template
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'
}
- [Django]-ValueError: Dependency on app with no migrations: customuser
- [Django]-Why does Django make migrations for help_text and verbose_name changes?
- [Django]-Django Rest Framework custom response message
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
- [Django]-Django 1.7 – App 'your_app_name' does not have migrations
- [Django]-Uploading large files with Python/Django
- [Django]-Turn off caching of static files in Django development server
- [Django]-Django REST Framework – pass extra parameter to actions
- [Django]-Django: Overriding AND extending an app template
- [Django]-ImportError: No module named virtualenv