Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django development server and MIME types

Is there anyway of setting MIME types in the django development server? Specifically, I would love to remove this message from the warnings in Chrome dev tools.

Resource interpreted as Image but transferred with MIME type image/x-png
like image 219
Fergal Moran Avatar asked Apr 30 '13 15:04

Fergal Moran


1 Answers

The Django development server uses the mimetypes module to guess the appropriate MIME type for any given file, and under the hood that module uses different configurations depending on your OS.

On Windows in particular it loads content types from HKEY_CLASSES_ROOT in the registry (for instance the key HKEY_CLASSES_ROOT\.js\Content Type for .js files) and it seems this configuration can often be set in ways you wouldn't expect (such as by opening a javascript file in a text editor)- it's not even Python or Django specific, since Go also uses the registry and is affected the same way.

Modifying the registry key for the affected file extensions should fix this issue without requiring any Django settings changes (however it is a system-wide configuration change that may not be desirable):

Windows Registry Editor Version 5.00

; Fixing the question's issue with PNG
[HKEY_CLASSES_ROOT\.png]
"Content Type"="image/png"

; Fixing a common problem with Javascript files
[HKEY_CLASSES_ROOT\.js]
"Content Type"="text/javascript"

On other operating systems MIME mappings are configured in a file that most users will never modify, so this tends not to be a problem. If needed however, mimetypes.knownfiles is a list of paths expected to contain MIME type mappings which could be edited as needed.

like image 133
tari Avatar answered Oct 26 '22 02:10

tari