How to set content type of JavaScript files in Django

后端 未结 4 918
野趣味
野趣味 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: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.

提交回复
热议问题