A few days ago, a netizen suggested that I add the RSS subscription function in the blog, feel very good, so I took a moment to see how to add RSS in Django, found that using Django Syndication feed framework is easy to implement.
The specific implementation steps and code are as follows:
1. Feed class
#-*-Coding:utf-8-*-from django.conf import settingsfrom django.contrib.syndication.views import Feedfrom django.utils . Feedgenerator import Rss201rev2feed from blog.models import articlefrom. Constants Import Sync_status class EXTENDEDRSSF Eed (rss201rev2feed): Mime_type = ' application/xml ' "" "Create a type of RSS feed which has content:encoded elements." "" De F Root_attributes (self): Attrs = Super (Extendedrssfeed, self). Root_attributes () attrs[' xmlns:content '] = '/HTTP/ purl.org/rss/1.0/modules/content/' return attrs def add_item_elements (self, Handler, item): Super (Extendedrssfeed, SEL f). add_item_elements (Handler, item) handler.addquickelement (U ' content:encoded ', item[' content_encoded ') class Latestarticlefeed (Feed): Feed_type = extendedrssfeed title = settings. Website_Name link = settings. Website_url author = settings. Website_Name Description = settings. Website_desc + u "focus on Python, Django, Vim, Linux, web development and Internet" def items (self): return Article.objects.filter (Hided=false, PublIshed=true, Sync_status=sync_status. synced). Order_by ('-publish_date ') [:] def item_extra_kwargs (self, item): Return {' content_encoded ': self.item_ Content_encoded (item)} def item_title (self, item): Return Item.title # Item_link are only needed if newsitem have no get_ Absolute_url method. def item_link (self, item): Return '/article/%s/'% item.slug def item_description (self, item): Return item.description def item_author_name (self, item): Return Item.creator.get_full_name () def item_pubdate (self, item): Return Item.publis H_date def item_content_encoded (self, item): Return item.content
2. URL Configuration
From Django import VERSION if Version[0:2] > (1, 3): From Django.conf.urls import patterns, include, Urlelse:from Dja Ngo.conf.urls.defaults import patterns, include, urlfrom. Feeds import latestarticlefeed urlpatterns = Patterns ("', URL (r ' ^feed/$ ', Latestarticlefeed ()),)