Django:RSS 和 ATOM 提要 Content-Type 标头?

发布于 2024-07-25 19:49:47 字数 322 浏览 6 评论 0原文

我跟着 本教程适用于 django 的 RSS 和 ATOM 提要,我让它工作了。

然而,测试开发服务器不断让浏览器将提要下载为文件,而不是浏览器将其检测为 xml 文档。

根据我的 HTTP 经验,Content-Type 标头中缺少 mime 类型。

我如何在 django 中指定它?

I followed along this tutorial for django's RSS and ATOM feeds and I got it to work.

However the test development server keeps making the browser download the feeds as a file instead of the browser detecting it as an xml document.

My experience with HTTP tells me that there is a missing mime type in the Content-Type header.

How do I specify that in django?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

冷︶言冷语的世界 2024-08-01 19:49:47

Everyblock 源代码中有对此的注释。

他们定义了一个类来替换标准 Django feed 的 mime 类型,如下所示:

# 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

There is a comment in the Everyblock source code about this.

They define a class that replaces the mime type of the standard Django feed like so:

# 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
感悟人生的甜 2024-08-01 19:49:47

您是否使用 rss 的可用视图?
这就是我在 urls.py 中的内容 - 我没有设置任何有关 mimetypes 的内容:

urlpatterns += patterns('',
    (r'^feeds/(?P<url>.*)/

其中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}
, 'django.contrib.syndication.views.feed', {'feed_dict': published_feeds}, 'view_name')`, )

其中published_feeds 类似于

Are you using the available view for rss?
This is what I have in my urls.py - and I am not setting anything about mimetypes:

urlpatterns += patterns('',
    (r'^feeds/(?P<url>.*)/

where published_feeds is something like

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}
, 'django.contrib.syndication.views.feed', {'feed_dict': published_feeds}, 'view_name')`, )

where published_feeds is something like

终止放荡 2024-08-01 19:49:47

当您创建 HTTPReponse 对象时,您可以指定其内容类型:

HttpResponse(content_type='application/xml')

或者任何实际的内容类型。

请参阅 http://docs.djangoproject。 com/en/dev/ref/request-response/#django.http.HttpResponse.__init__

When you create an HTTPReponse object you can specify its content-type:

HttpResponse(content_type='application/xml')

Or whatever the content type actually is.

See http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpResponse.__init__

香橙ぽ 2024-08-01 19:49:47

我猜问题出在 OS X 上的 Camino 浏览器上,而不是 HTTP 标头和 mime 类型上。

当我尝试使用 Safari 时,它成功了。

I guess the problem was with the Camino browser on OS X, not with the HTTP header and mime type.

When I tried on Safari, it worked.

安人多梦 2024-08-01 19:49:47

9 年后,我在使用 Firefox 和 Django 2.1 时仍然遇到了这个问题。

上面的解决方案并没有解决它,所以我最终使用了这个:

class XMLFeed(Feed):
    def get_feed(self, obj, request):
        feedgen = super().get_feed(obj, request)
        feedgen.content_type = 'application/rss+xml; charset=utf-8'  # New standard
        # feedgen.content_type = 'application/xml; charset=utf-8'  # Old standard, left here for reference
        return feedgen

使用此类而不是 Feed 将 mime-type 设置为所需的“application/rss+xml”。

更新

我注意到mime类型“application/xml”已经过时,应该使用“application/rss+xml”。 上面的代码已相应更新,但尚未测试。

I still encountered this problem, 9 years later with Firefox and Django 2.1.

The solutions above didn't cut it, so I ended up using this:

class XMLFeed(Feed):
    def get_feed(self, obj, request):
        feedgen = super().get_feed(obj, request)
        feedgen.content_type = 'application/rss+xml; charset=utf-8'  # New standard
        # feedgen.content_type = 'application/xml; charset=utf-8'  # Old standard, left here for reference
        return feedgen

Using this class instead of Feed sets the mime-type to 'application/rss+xml' as wanted.

Update

It was brought to my attention that mime-type 'application/xml' is outdated, and 'application/rss+xml' should be used instead. The code above was updated accordingly but has yet to be tested.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文