Generating feeds per tag in Django
Adding Feeds
One of the nice features of a website is the ability to have RSS or Atom feeds for say articles, blog entries, photos or links that you newly published. If your website is made with Django life gets easier, it has a built-in module django.contrib.syndication that handle basic syndication functionality.
Let’s create a feed for our imaginary web app (a blog):
Inside our web app blog create a new file feeds.py
What the above codes does is that it will output the latest 10 live entry items, you can always adjust items(self) method. Generating the feeds per tag is now simply sub classing LatestEntriesFeed and overriding some functions and variables, I use django-tagging so the Tag object must be imported see below:
The next thing to do after defining our feed classes is to register this feed in project root urls.py:
and add these inside urlpatterns
(r'^feeds/(?P<url>.*)/$', 'django.contrib.syndication.views.feed', \
{'feed_dict': feeds}, ),
Finally we need to set up the templates feeds/latest_title.html and feeds/latest_description.html, and for tags feeds/tag_title.html and feeds/tag_description.html inside the templates directory.
feeds/latest_title.html
{{ obj.title }}
feeds/latest_description.html
{{ obj.body_html|truncatewords_html:"50"|safe }}
feeds/tag_title.html
{{ obj.title }}
feeds/tag_description.html
{{ obj.body_html|truncatewords_html:"50"|safe }}
That’s it, you can also create feeds per categories.