6

I am newbie in Django, and i was working on a Django app. And i used "media" folder to keep my css and javascript files (i know it's not a good practice) but it was working. Some lines of code

<link href="/media/shop/bootstrap//css/bootstrap.min.css" rel="stylesheet">
<link href="/media/shop/bootstrap//css/bootstrap-responsive.min.css" rel="stylesheet">
<link href="/media/shop/themes/css/bootstrappage.css" rel="stylesheet" />

<script src="/media/shop/themes/js/jquery-1.7.2.min.js"></script>
<script src="/media/shop/bootstrap//js/bootstrap.min.js"></script>
<script src="/media/shop/themes/js/superfish.js"></script>

However what happened i don't know after somedays(today) when i opened this project again and tried to run on local host then i am getting error in console like this
this.
After clicking on the link present in error it displays my javascript file but not executing it.
And the most weired thing is that when i executed the same file in other system(also on local host) after transferring the same folder then it is working, no any such error in console. And the same project(website) is also hosted on internet and when i open that site online in my system again then also no any such error. However when i replace

<script src="/media/shop/themes/js/jquery-1.7.2.min.js"></script>

with

<script type="javascript" src="/media/shop/themes/js/jquery-1.7.2.min.js"></script>

then error not comes in console but it is still not working. I don't think there is any problem with browser setting because i checked in 3 different browsers but it is showing same thing.
Any suggestion how to solve this issue and how to disable mime type checking ?

Abhishek Pratap Singh
  • 613
  • 3
  • 11
  • 18
  • What are your django settings? You should be serving static files via the static loader and not media – Airith May 11 '20 at 17:47
  • Any answer found for this issue ? – ThivankaW Mar 31 '21 at 08:57
  • I had this issue in the admin site after switching to another database, installing the whitenoise library helped me https://stackoverflow.com/a/72035243/7830459 – Alex Li Jun 10 '23 at 03:00

3 Answers3

3

Try adding the following code to your settings.py

import mimetypes

mimetypes.add_type("text/javascript", ".js", True)

It's a solution someone told me. Sadly, it does not come with any explanation.

Jonathan Tsai
  • 224
  • 1
  • 7
  • 1
    Some explanation for what this does: The reason there was an error message in the first place is because Windows analyzes each file to determine what kind of file it is, and for javascript it decides it's a textfile (subtype "plain"). You browser assumes this to be true, and refuses to execute something that is just plain text. Your python code tells Windows anything with extension "js" is actually subtype javascript, something your browser will execute. By the way, other OSes should always correctly determine its type. And I know I'm late, but for others reading this answer. – Emil Bode Jul 01 '22 at 08:08
0

The script should be type="application/javascript"

Airith
  • 2,024
  • 1
  • 14
  • 10
-1

Check your Windows registry key.

C:\>reg query HKCR\.js /v "Content Type"

HKEY_CLASSES_ROOT\.js
    Content Type    REG_SZ    text/plain

Update the key as following, it works for me.

C:\>reg add HKCR\.js /v "Content Type" /t REG_SZ /d application/javascript
Sam Fu
  • 1