Django:RSS和ATOM提供Content-Type標題?

[英]Django: RSS and ATOM feeds Content-Type header?


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

我按照本教程了解了django的RSS和ATOM提要,然后我開始工作了。

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.

但是,測試開發服務器不斷使瀏覽器將提要下載為文件,而不是瀏覽器將其檢測為xml文檔。

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

我使用HTTP的經驗告訴我,Content-Type標頭中缺少mime類型。

How do I specify that in django?

如何在django中指定?

5 个解决方案

#1


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

Everyblock源代碼中有關於此的注釋。

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

它們定義了一個替換標准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

#2


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:

你使用rss的可用視圖嗎?這就是我在urls.py中的內容 - 而且我沒有設置任何關於mimetypes的內容:

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

where published_feeds is something like

其中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}

#3


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

創建HTTPReponse對象時,可以指定其內容類型:

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__

#4


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

我想問題是OS X上的Camino瀏覽器,而不是HTTP頭和mime類型。

When I tried on Safari, it worked.

當我嘗試Safari時,它起作用了。

#5


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

我仍然遇到這個問題,9年后使用Firefox和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/xml; charset=utf-8'
        return feedgen

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

使用此類而不是Feed將mime-type設置為“application / xml”。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/06/27/7299fefcc8e68ec04359f847e9c19a8f.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com