1

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?

Net Citizen
  • 5,174
  • 8
  • 38
  • 50
  • I know there is a mime type parameter for render_to_response but that is not used for django's syndication. – Net Citizen Jun 27 '09 at 23:11
  • Django `contrib.syndication` sets proper content-type headers for feed. Check it one more time with more precise tool than raw browser. – Alex Koshelev Jun 28 '09 at 07:40
  • I've got the same problem in Firefox. mimetype should be application/atom+xml for ATOM. When I'm calling page using GET /page/rss/ it works fine but on POST FF trying to download the damn file. – tefozi Jul 03 '09 at 22:44

5 Answers5

10

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
David
  • 291
  • 2
  • 4
3

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

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}
rob
  • 36,896
  • 2
  • 55
  • 65
1

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__

dbb
  • 1,100
  • 6
  • 12
1

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.

Net Citizen
  • 5,174
  • 8
  • 38
  • 50
1

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.

Shovalt
  • 6,407
  • 2
  • 36
  • 51
  • But note that 9 years later, the only _correct_ mimetype is still `application/rss+xml` as per the [official RSS spec](http://www.rssboard.org/rss-mime-type-application.txt). Changing the header to send `application/xml` will in fact cause spec-compliant RSS readers to potentially reject that URL now. – Mike 'Pomax' Kamermans Sep 20 '19 at 21:35
  • Thanks for bringing it to my attention @Mike'Pomax'Kamermans, I updated the answer. Bear in mind that the main difference between my answer and the previous ones was setting the content type within `feedgen` object, since other methods simply did not work for me. The content type itself is the easy part as far as I'm concerned. – Shovalt Sep 22 '19 at 07:33
  • @Shovalt not related to this issue, but were you able to make the pagination work ? It seems django is not handling pagination in syndication feed. – Sandeep Balagopal Nov 01 '19 at 06:07
  • 1
    @SandeepBalagopal tbh I don't really understand the concept of pagination in feeds. My needs were always either to return all available items, or a limited number of (most recent) items, as I expect in RSS feeds I follow, as well as podcasts. – Shovalt Nov 01 '19 at 08:27