Creating feeds for your blog posts

Django has a built-in syndication feed framework that you can use to dynamically generate RSS or Atom feeds in a similar manner to creating sitemaps using the sites framework.

Create a new file in your blog application directory and name it feeds.py. Add the following lines to it:

from django.contrib.syndication.views import Feed
from django.template.defaultfilters import truncatewords
from .models import Post

class LatestPostsFeed(Feed):
    title = 'My blog'
    link = '/blog/'
    description = 'New posts of my blog.'

    def items(self):
        return Post.published.all()[:5]

    def item_title(self, item):
        return item.title

    def item_description(self, item):
        return truncatewords(item.body, 30)

First, we subclass the Feed class of the syndication framework. The title, link, and description attributes correspond to the <title>, <link>, and <description> RSS elements respectively.

The items() method retrieves the objects to be included in the feed. We are retrieving only the last five published posts for this feed. The item_title() and item_description() methods receive each object returned by items() and return the title and description for each item. We use the truncatewords built-in template filter to build the description of the blog post with the first 30 words.

Now, edit the urls.py file of your blog application, import the LatestPostsFeed you just created, and instantiate the feed in a new URL pattern:

from .feeds import LatestPostsFeed

urlpatterns = [
    # ... 
    url(r'^feed/$', LatestPostsFeed(), name='post_feed'),
]

Navigate to http://127.0.0.1:8000/blog/feed/ in your browser. You should now see the RSS feedincluding the last five blog posts:

<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <title>My blog</title>
    <link>http://127.0.0.1:8000/blog/</link>
    <description>New posts of my blog.</description>
    <atom:link href="http://127.0.0.1:8000/blog/feed/" rel="self"/>
    <language>en-us</language>
    <lastBuildDate>Sun, 20 Sep 2015 20:40:55 -0000</lastBuildDate>
    <item>
      <title>Who was Django Reinhardt?</title>
      <link>http://127.0.0.1:8000/blog/2015/09/20/who-was-django-reinhardt/</link>
      <description>The Django web framework was named after the amazing jazz guitarist Django Reinhardt.</description>
      <guid>http://127.0.0.1:8000/blog/2015/09/20/who-was-django-reinhardt/</guid>
    </item>
	 ...
  </channel>
</rss>

If you open the same URL in a RSS client, you will be able to see your feed with a user-friendly interface.

Last step is adding a feed subscription link to the blog's sidebar. Open the blog/base.html template and add the following line under the number of total posts inside the sidebar div:

<p><a href="{% url "blog:post_feed" %}">Subscribe to my RSS feed</a></p>

Now, open http://127.0.0.1:8000/blog/ in your browser and take a look at the sidebar. The new link should take you to your blog's feed:

Creating feeds for your blog posts
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset