Django 开发服务器可以正确地为 SVG 提供服务吗? [英] Can the Django dev server correctly serve SVG?

查看:18
本文介绍了Django 开发服务器可以正确地为 SVG 提供服务吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下方法提供 svg 地图:

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>

在 Firefox 中,这会导致出现插件提示.如果我将 map.svg 重命名为 map.xml,它会正确显示图像.我认为这是因为 Django 的开发服务器(特别是 django.views.static.serve)没有为 svg 提供正确的 MIME 类型.这是问题吗,如果是,是否有补丁?

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?

推荐答案

我目前没有可用的 Django 来测试这个,但看起来静态服务器使用 mimetypes library 来确定内容类型(特别是 guess_type()).

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()).

通过谷歌搜索,我发现了一些代码,您可能会在设置中添加这些代码.py 添加对 svg 内容类型的支持:

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)

还有这个博文 特定于 Pylons 但它提到了一个类似的问题.他指定 MIME 类型存储在/etc/mime.types"中,并且缺少 SVG,因为它不是官方 MIME 类型.他可能是对的,因为我在 IANA<上的任何地方都找不到 SVG 的 MIME 类型/a>.

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.

这篇关于Django 开发服务器可以正确地为 SVG 提供服务吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆