ジャンゴ:RSS および ATOM フィードの Content-Type ヘッダー?
-
20-08-2019 - |
質問
私もついて行きました このチュートリアル django の RSS フィードと ATOM フィードを試してみたところ、動作するようになりました。
ただし、テスト開発サーバーは、ブラウザーがフィードを xml ドキュメントとして検出するのではなく、ブラウザーにフィードをファイルとしてダウンロードさせ続けます。
HTTP に関する私の経験から、Content-Type ヘッダーに MIME タイプが欠落していることがわかります。
ジャンゴでそれを指定するにはどうすればよいですか?
解決 4
問題は HTTP ヘッダーと MIME タイプではなく、OS X の Camino ブラウザーにあったと思います。
Safariで試してみたところ、うまくいきました。
他のヒント
これについては、Everyblock のソース コードにコメントがあります。
標準の Django フィードの 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
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://docs.djangoproject.com/en/dev/ref/request-response/#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 タイプを「application/xml」に設定します。