views:

246

answers:

2

I am trying to serve a svg map using:

<object data="map.svg" type="image/svg+xml" width="400" height="300">
    <embed src="map.svg" type="image/svg+xml" width="400" height="300" />
</object>

In Firefox this leads to a plugin prompt. If I rename map.svg to map.xml it shows the image correctly. I assume this is because the Django's dev server (specifically django.views.static.serve) is not serving the svg with the correct mime-type. Is this the problem, and if so, is there a patch?

+3  A: 

If you're serving the SVG dynamically from a regular django view, you can specify the mimetype in the HTTPResponse object you return from that view. In this case, you'll want the mimetype in place for both dev and production use:

def myview(request):
    svg_data = generate_some_svg_data()
    return HttpResponse(svg_data, mimetype="image/svg+xml")
Jarret Hardie
No the svg is static. So I was hoping Django static serve would take care of it.
Jason Christa
`static.serve` uses python's built-in mimetypes module (as Lance McNearney mentions in his post) to guess the mimetype based on the filename... in my case, it seems to guess correctly (`import mimetypes; print mimetypes.guess_type('map.svg')`. Can you verify using firebug that this is the mimetype being sent with the map.svg file?
Jarret Hardie
I get (None, None) with Python 2.6 on Windows.
Jason Christa
Interesting! Good to know. On OS X and ubuntu I get 'image/svg+xml' out of the box. Delving into the python source for mimetypes.py, I see that it has a default list of types that does not include svg, and that it has paths to additional type listing helpers for unix-like systems. I never knew this... thanks for your question.
Jarret Hardie
+4  A: 

I don't have Django available to test this at the moment but it looks like the static server uses the mimetypes library to determine the content type (specifically guess_type()).

With a little bit a Googling, I came across some code that you could probably throw in your settings.py to add support for the svg content type:

import mimetypes

mimetypes.add_type("image/svg+xml", ".svg", True)
mimetypes.add_type("image/svg+xml", ".svgz", True)

There's also this blog post specific to Pylons but it mentions a similar issue. He specifies that the MIME types are stored in "/etc/mime.types" and that SVG is missing because it's not an official MIME type. He may be right, since I can't find a MIME-type for SVG anywhere on the IANA.

Lance McNearney
That did the trick. Hopefully those get added to mimetypes lib.
Jason Christa