Offering an RSS feed
Tech blogs are often consumed by robots, in particular by feed readers. If we want to build a feed in Django, it’s really convenient. Django incorporates a framework called Syndication that automates tasks such as dynamic generation of XML, fields, and caching.
In app/website/feed.py
, we add the following class that inherits from Feed
:
from django.contrib.syndication.views import Feed from django.urls import reverse from .models import Post class LatestEntriesFeed(Feed): title = "My blog" link = "/feed/" description = "Updates to posts." def items(self): return Post.objects.all()[:5] def item_title(self, item): return item.title def item_description(self, item): ...