5
votes

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".

enter image description here

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")
4
Best dupe I could find quickly, but really... you are setting headers and content types in Python. Django has nothing to do with this AFAIK - rlemon
At stackoverflow.com/questions/3467404/… someone said "It means that the server is sending an Javascript HTTP response with content-type:text/plain.". I'm using the built-in web server of Django (python manage.py runserver), hence I assume that I have to configure Django's built-in server to fix this problem. - Mentiflectax
I don't know enough about Python or Django to comment on that technology, but in the end, you need to find a way that when requested, those uri's give a correct header type. If Django has or requires that, ok, if not, what is serving those files? That's the question. - Jared Farrish
On the same page, someone wrote that "AddType text/javascript .js" would fix the issue on the Apache server (all files with the .js extension would have "text/javascript" content-type). I need something similar for Django's built-in web server. - Mentiflectax

4 Answers

8
votes

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.

12
votes

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.

0
votes

Since this doesn't prevent scripts from being interpreted correctly by the browser, why is this a problem? runserver is only for development (not production use), and as such is not a full blown web server.

You should continue to use it in development and when you move to production configure your webserver appropriately for static files.

However, if you absolutely must use the development server to serve static files; see how to serve static files.

0
votes

For Django use request context in views :

return render_to_response('success.html', {'object': varobject},context_instance=RequestContext(request))