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 mine urls.py
and something like
class MyFeed(Feed):
...
in my feeds.py
.
I want to redirect traffic from this feed to FeedBurner. I have to do this in Django as there is no mod_rewrite on my server.
I think I should change the entry urls.py
to
(r'^feeds/(?P<url>.*)/$', 'feeds.redirect', {'feed_dict': feeds})
and add 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 work as i get the following error (you must change ==
to !=
to see this):
NoReverseMatch in / feeds / myfeed /
The inverse for '
<function feed at 0x16a2430>
' with arguments' () 'and keyword arguments' {' url ': u'myfeed', 'feed_dict': {'myfeed':<class 'feeds.MyFeed'>
}} 'was not found.
How can this be solved?
a source to share
The problem is that you removed the link django.contrib.syndication.views.feed
from your urls.py.
Instead of using reverse to redirect to a different url, try simply reversing 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')
a source to share
Well I guess it helps someone wondering what is the correct way to do it in django 1.3+
from django.http import HttpResponseRedirect
from feeds import MyFeed #your feed class, check https://docs.djangoproject.com/en/1.3/ref/contrib/syndication/
def burnedFeed(request, **kwargs):
if request.META['HTTP_USER_AGENT'].startswith('FeedBurner'):
feed = MyFeed()
return feed(request)
else:
return HttpResponseRedirect('http://feeds2.feedburner.com/MyFeedName')
a source to share