Using @cache_page With Django's Feeds
When returning a django.contrib.syndication.Feed
from a view function, the @cache_page
decorator does not seem to work as expected. Briefly inspecting the source of Feed
explains why: Feed
is not an HttpResponse
. Truly, this isn't a bug: it's a documentation (if not comprehension) issue. Because the view function is expected to return an HttpResponse
object, the Feed
must first be converted.
Notice that Feed
is a callable object, returning its own state as the necessary HttpResponse
. As such, you may, indeed, use the @cache_page
decorator on any view method that returns a Feed
:
@cache_page(60*5)
def latest_feed(request):
return SomeFeed(some, params)(request)
Alternatively, using the cache_page
decorator in your urls
module will accomplish the same task:
urlpatterns = patterns('',
url('^somepath/$', cache_page(SomeFeed(some, params), 60*5))
)
This works because the URL specifies a callable. More information is available in the Django documentation.