- Django 2 by Example
- Antonio Melé
- 392字
- 2021-06-25 21:19:07
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 site's framework. A web feed is a data format (usually XML) that provides users with frequently updated content. Users will be able to subscribe to your feed using a feed aggregator, a software that is used to read feeds and get new content notifications.
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 blog/urls.py file, import LatestPostsFeed you just created, and instantiate the feed in a new URL pattern:
from .feeds import LatestPostsFeed
urlpatterns = [
# ...
path('feed/', LatestPostsFeed(), name='post_feed'),
]
Navigate to http://127.0.0.1:8000/blog/feed/ in your browser. You should now see the RSS feed, including 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://localhost:8000/blog/</link>
<description>New posts of my blog.</description>
<atom:link href="http://localhost:8000/blog/feed/" rel="self"/>
<language>en-us</language>
<lastBuildDate>Fri, 15 Dec 2017 09:56:40 +0000</lastBuildDate>
<item>
<title>Who was Django Reinhardt?</title>
<link>http://localhost:8000/blog/2017/12/14/who-was-django-
reinhardt/</link>
<description>Who was Django Reinhardt.</description>
<guid>http://localhost:8000/blog/2017/12/14/who-was-django-
reinhardt/</guid>
</item>
...
</channel>
</rss>
If you open the same URL in an RSS client, you will be able to see your feed with a user-friendly interface.
The final step is to add 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 p:
<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: