Reputation: 18730
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")
Upvotes: 9
Views: 14169
Reputation: 20130
The below code has worked for me. I think mime type has been deprecated in the recent django version?
response = HttpResponse("alert('hello')")
content_type="application/x-javascript") return response
Upvotes: 0
Reputation: 835
The solution to the problem is describe in the documentation
HKEY_CLASSES_ROOT\.js\Content Type
to text/javascript
.Upvotes: 0
Reputation: 115
I ran into that error today even after adding @piephai s solution to my settings.py. I then noticed that @Daniel Roseman got it right as well: My import paths were wrong, I had to add ".js" to all of them, for example:
import {HttpTool} from "./requests";
became import {HttpTool} from "./requests.js";
Makes sense after thinking about how routes for static files are generated.
Upvotes: 0
Reputation: 21
Expanding on Alexandre's answer using put the below code into settings.py of your main project. After that you will need to clear your browser cache (you can test it by opening an incognito window as well) in order to get the debug panel to appear.
if DEBUG:
import mimetypes
mimetypes.add_type("application/javascript", ".js", True)
Upvotes: 2
Reputation: 528
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
.
Upvotes: 19
Reputation: 1
For Django use request context in views :
return render_to_response('success.html', {'object': varobject},context_instance=RequestContext(request))
Upvotes: 0
Reputation: 599866
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.
Upvotes: 9
Reputation: 174682
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.
Upvotes: 0