How to set content type of JavaScript files in Django

后端 未结 4 917
野趣味
野趣味 2021-01-13 07:48

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

相关标签:
4条回答
  • 2021-01-13 08:09

    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.

    0 讨论(0)
  • 2021-01-13 08:12

    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 讨论(0)
  • 2021-01-13 08:19

    For Django use request context in views :

    return render_to_response('success.html', {'object': varobject},context_instance=RequestContext(request))
    
    0 讨论(0)
  • 2021-01-13 08:30

    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 讨论(0)
提交回复
热议问题