Opened 3 years ago

Closed 3 years ago

#20162 closed Cleanup/optimization (fixed)

Document `static.serve()` limitation concerning missing system wide mimetypes.

Reported by: julien Owned by: julien
Component: Documentation Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


There's a bug in Python 2.6 where the mimetypes module returns the wrong value for SVG files:

Python 2.6.6 (r266:84292, Feb 22 2013, 00:00:18) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
>>> import mimetypes
>>> mimetypes.guess_type('blah.svg')
(None, None)

This is fixed in Python 2.7:

Python 2.7.2 (default, Mar  2 2012, 16:57:52) 
[GCC 4.2.1 Compatible Apple Clang 3.1 (tags/Apple/clang-318.0.45)] on darwin
>>> import mimetypes
>>> mimetypes.guess_type('blah.svg')
('image/svg+xml', None)

Django's static.serve() view should be made smarter to always return image/svg+xml regardless of the Python version.

Change History (6)

comment:1 Changed 3 years ago by charettes

  • Resolution set to invalid
  • Status changed from new to closed

It looks like .svg was only included in the default mimetypes.types_map in python 3.2.

I guess running mimetype blah.svg in a terminal on your Red Hat box wouldn't return any result either.

It worked on python 2.6 on my machine:

Python 2.6.8 (default, Sep 21 2012, 13:41:09) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mimetypes
>>> mimetypes.guess_type('blah.svg')
('image/svg+xml', None)

Because the mimetypes.types_map is populated from different files on your OS (/etc/mime.types on my box) which contains mimetypes mappings.

All in all this is not a Django issue nor a Python one. You should either configure your OS or issue a mimetypes.add_type('image/svg+xml', '.svg') within your app.

comment:2 Changed 3 years ago by julien

I disagree. Although at the root this might be an issue with the OS, Python can step up to get around the issue — just like it did with that commit in Python 3.2. Similarly, I believe Django should do the same since it supports Python >= 2.6 and SVG is now an essential part of the Web. I think that by doing this Django would fill its role as a Web framework.

Chrome (and probably other browsers) simply will not render SVG images if they are not served with the right mimetype. Using mimetype.add_type() in my app just because I'm using a Red Hat box feels like a counter-intuitive hack to me. In my mind Django should just work for this type of trivial things, regardless of the OS it is served from.

Let's catch up and discuss this on IRC. Cheers!

comment:3 Changed 3 years ago by claudep

I think I would also categorize that in platform weakness. If the issue can simply be resolved by adding the proper mime type in /etc/mime.types on your platform, I'm not in favour of fixing it in Django. Fixing it at platform level has also the advantage that it might also solve the issue for any Web server (in the case they use that information source also, not verified).

comment:4 Changed 3 years ago by charettes

  • Component changed from Core (Other) to Documentation
  • Resolution invalid deleted
  • Status changed from closed to new
  • Type changed from Bug to Cleanup/optimization
  • Version changed from 1.5 to master

As discussed with claudep and julien on IRC I'm re-opening as a documentation enhancement.

We should document static.serve() limitation and how to properly solves it by configuring your system.

comment:5 Changed 3 years ago by charettes

  • Summary changed from Wrong mimetype for SVG files under Python 2.6 to Document `static.serve()` limitation concerning missing system wide mimetypes.

comment:6 Changed 3 years ago by Julien Phalip <jphalip@…>

  • Resolution set to fixed
  • Status changed from new to closed

In ffc8e2e0ae9f1e35f4b7c78e6235bd0e3ba41aa9:

Fixes #20162 -- Added a note in the documentation for static.serve() about the need for updating the system's map files when incorrect content types are returned. Many thanks to Simon Charette and Claude Paroz for their feedback.

Note: See TracTickets for help on using tickets.
Back to Top