Search code examples
djangofeedburner

Redirect Django feed to FeedBurner


I have an Atom feed set up according to http://docs.djangoproject.com/en/dev/ref/contrib/syndication/ which means I have something like

(r'^feeds/(?P<url>.*)/$', 'django.contrib.syndication.views.feed', {'feed_dict': feeds})

in my urls.py and something like

class MyFeed(Feed):
    ...

in my feeds.py.

I'd like to redirect traffic from this feed to the FeedBurner. I have to do this in Django as there is no mod_rewrite on my server.


I guess I should change urls.py entry to

(r'^feeds/(?P<url>.*)/$', 'feeds.redirect', {'feed_dict': feeds})

and supplement feeds.py with

from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse

def redirect(request, **kwargs):
    if request.META['HTTP_USER_AGENT'] == 'FeedBurner':
        view = 'django.contrib.syndication.views.feed'
        return HttpResponseRedirect(reverse(view, kwargs=kwargs))
    else:
        return HttpResponseRedirect('http://feeds2.feedburner.com/MyFeed')

but it doesn't seem to work as I get the following error (you have to change == to != to see this):

NoReverseMatch at /feeds/myfeed/

Reverse for '<function feed at 0x16a2430>' with arguments '()' and keyword arguments '{'url': u'myfeed', 'feed_dict': {'myfeed': <class 'feeds.MyFeed'>}}' not found.

How can this be solved?


Solution

  • The problem here is that you removed the django.contrib.syndication.views.feed reference from your urls.py.

    Instead of using reverse to redirect to a different URL, try just returning the feed from your existing view:

    from django.http import HttpResponseRedirect
    from django.core.urlresolvers import reverse
    from django.contrib.syndication.views import feed
    
    def redirect(request, **kwargs):
        if request.META['HTTP_USER_AGENT'].startswith('FeedBurner'):
            return feed(request, **kwargs)
        else:
            return HttpResponseRedirect('http://feeds2.feedburner.com/MyFeed')