- 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:

- 社交電商運(yùn)營(yíng)策略、技巧與實(shí)操
- 網(wǎng)絡(luò)的琴弦:玩轉(zhuǎn)IP看監(jiān)控
- 6G新技術(shù) 新網(wǎng)絡(luò) 新通信
- Kong網(wǎng)關(guān):入門、實(shí)戰(zhàn)與進(jìn)階
- Getting Started with Memcached
- 數(shù)據(jù)血緣分析原理與實(shí)踐
- Implementing NetScaler VPX?
- TCP/IP基礎(chǔ)(第2版)
- 全聯(lián)網(wǎng)標(biāo)識(shí)服務(wù)
- 現(xiàn)場(chǎng)綜合化網(wǎng)絡(luò)運(yùn)營(yíng)與維護(hù):運(yùn)營(yíng)商數(shù)字化轉(zhuǎn)型技術(shù)與實(shí)踐
- Corona SDK Application Design
- 智慧的物聯(lián)網(wǎng):感知中國(guó)和世界的技術(shù)
- 無(wú)線傳感器網(wǎng)絡(luò)定位方法及應(yīng)用
- 網(wǎng)絡(luò)分析技術(shù)揭秘:原理、實(shí)踐與WinPcap深入解析
- 5G智聯(lián)萬(wàn)物:輕松讀懂5G應(yīng)用與智能未來(lái)