Question

Django deployed app shows following error: "Refused to execute script from "url/file.js" because its MIME type"

I followed this deployment tutorial https://docs.digitalocean.com/tutorials/app-deploy-django-app/ and after trying to add staticfiles I get the title’s error. This is the static configuration:

STATIC_URL = 'staticfiles/' # I changed this from 'static' because I tought it could work but it didn't.
# STATIC_URL = "/static/"
# Add these new lines
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

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

This is how the file is requested on django template:

{% load static %}
<script src="{% static 'appointment-scripts.js' %}"></script>

And this is how it is rendered by browser:

<script src="/staticfiles/appointment-scripts.js"></script>

In my directory the file “appointment-scripts.js” exists so I think that the problem is some configuration on my app, but I just don’t know how to get more information about this, any help would be appreciated.


Submit an answer
Answer a question...

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Well, I think this question was kind a newbie one. But if there is someone trying to deploy his first application I think that these line will be helpfull:

Static files served from our own server works well for testing purposes but on production is not recommended, if you try to serve static files without changes from your own server maybe you will get my error, so you need to upload them to another part (maybe another server), this is very tedious so fortunately someone (@_EvansD) developed “whitenoise” a really timesaving that allows your web app to serve its own static files. In my case I just have to do the following:

Install whitenoise

pip install whitenoise

Modify setting.py

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware', # Add here the whitenoise middleware app
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

# Add these at the bottom of you settings.py file
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

That’s all.