我跟着 这个教程 对于它的RSS和原子的饲料,并且我得到了它的工作。

但是测试发展服务器保存使浏览器下载源作为一个文件,而不是浏览器检测它作为一种xml文件。

我的经验,HTTP告诉我,有一个失踪mime type在Content-Type header.

我如何指定,在django?

有帮助吗?

解决方案 4

我想问题是与在OS X的卡米诺浏览器,不与HTTP报头和MIME类型。

当我试图在Safari,它的工作。

其他提示

有在约此Everyblock源代码评论。

它们定义了一类取代喂像这样mime类型的标准的Django的:

# RSS feeds powered by Django's syndication framework use MIME type
# 'application/rss+xml'. That's unacceptable to us, because that MIME type
# prompts users to download the feed in some browsers, which is confusing.
# Here, we set the MIME type so that it doesn't do that prompt.
class CorrectMimeTypeFeed(Rss201rev2Feed):
    mime_type = 'application/xml'

# This is a django.contrib.syndication.feeds.Feed subclass whose feed_type
# is set to our preferred MIME type.
class EbpubFeed(Feed):
    feed_type = CorrectMimeTypeFeed

您使用RSS可用的看法? 这是我在我的urls.py - 我不是着手MIME类型什么:

urlpatterns += patterns('',
    (r'^feeds/(?P<url>.*)/$', 'django.contrib.syndication.views.feed', {'feed_dict': published_feeds}, 'view_name')`,
)
其中published_feeds

是一样的东西

class LatestNewsFeed(Feed):
    def get_object(self, bits):
      pass

    def title(self, obj):
      return "Feed title"

    def link(self, obj):
      if not obj:
        return FeedDoesNotExist
      return slugify(obj[0])

    def description(self, obj):
      return "Feed description"

    def items(self, obj):
      return obj[1]

published_feeds = {'mlist': LatestNewsFeed}

当创建你可以指定它的内容类型的HTTPReponse对象:

HttpResponse(content_type='application/xml')

或任何内容类型实际上是

请参阅 HTTP://文档.djangoproject.com / EN的/ dev / REF /请求响应/#django.http.HttpResponse .__ init__

我还是使用Firefox和Django的2.1遇到了这个问题,第9年后。

在上面的解决方案并没有削减,所以我结束了使用这样的:

class XMLFeed(Feed):
    def get_feed(self, obj, request):
        feedgen = super().get_feed(obj, request)
        feedgen.content_type = 'application/xml; charset=utf-8'
        return feedgen

使用这个类的代替Feed设置mime类型为想“应用/ XML”。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top