11

I have a Django app hosted on Heroku, and my stylesheet isn't loading. Now I've taken the time to read the other questions on this issue, but I believe each situation is unique. Now the error is as follows:

Refused to apply style from 'https://mazzodjangoapp.herokuapp.com/static/blog/main.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.

The static directory is defined in my settings.py file as:

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

In my base.html file, my link tag looks like this:

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

Works locally. Why is it not loading up in the Heroku environment?

5 Answers 5

6

As per the Heroku guide for configuring Django apps, you need to use pip and install django_heroku

pip install django_heroku

Add it to your settings.py

import django_heroku

And finally, add this to the bottom of the settings.py file

django_heroku.settings(locals())
Sign up to request clarification or add additional context in comments.

Comments

4

"whitenoise" can solve "MIME type" error to load CSS successfully:

This is how you use "whitenoise":

First, install "whitenoise":

pip install whitenoise

Then, set it to "MIDDLEWARE" in "settings.py". That's it to load CSS successfully:

MIDDLEWARE = [
    # ...
    "django.middleware.security.SecurityMiddleware",
    "whitenoise.middleware.WhiteNoiseMiddleware", # Here
    # ...
]

Comments

3

Using white noise in my project worked for me. since I had system errors and I couldn't install Django-Heroku.

Here is the link I used to set up my white noise.

Basically

  1. Install white noise

    pip install whitenoise

  2. Add it to middleware

    MIDDLEWARE = [
    
    django.middleware.security.SecurityMiddleware',
    
    'whitenoise.middleware.WhiteNoiseMiddleware',
    
     #...
    
    ]
    

and that's all. you then send your code to Heroku

Comments

2

You need to run $ python manage.py collectstatic before pushing to Heroku.

1 Comment

For anyone deploying a Django app to DigitalOcean... this is also true. You cannot run collectstatic from the console... you need to include the output in your git repo.
0

make sure that DEBUG = False in settings.py

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.