I have a Django application, which requires several JavaScript files.
In Chrome I get the error "Resource interpreted as Script, but transferred with MIME type text/html".
AFAIK (see 2) in order to fix this problem, I need to configure Django so that JavaScript files are returned with content-type "application/x-javascript".
How can I do this in Django?
UPDATE: I followed the advice by Daniel Roseman and found following solution.
1) Modify urls.py:
urlpatterns = patterns('',
...
url(r'.*\.js$', java_script),
...
)
2) Add following function to views.py:
def java_script(request):
filename = request.path.strip("/")
data = open(filename, "rb").read()
return HttpResponse(data, mimetype="application/x-javascript")
I had an issue with Django serving javascript files as text/plain with the included server, which doesn't work too well with ES6 modules. I found out here that you could change file extension associations by placing the following lines in your settings.py:
#settings.py
if DEBUG:
import mimetypes
mimetypes.add_type("application/javascript", ".js", True)
and javascript files were now served as application/javascript
.
I suspect the problem is not what you think it is. What is probably actually happening is that your JS files are not being served at all: instead, the Django error page is being sent. You need to figure out why.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With