A few days ago, a netizen suggested that I add RSS subscriptions to the blog, feel good, so I took the time to see how to add RSS in Django, found that the use of Django syndication feed framework is easy to achieve.
The implementation steps and code are as follows:
1, Feed class
#-*-Coding:utf-8-*-from django.conf Import settings from django.contrib.syndication.views import feeds from Django.ut Ils.feedgenerator Import rss201rev2feed from blog.models import Article from. Constants Import Sync_status class Ext Endedrssfeed (rss201rev2feed): Mime_type = ' application/xml ' "" "Create a type of RSS feed that has content:encoded elem
Ents. "" "Def 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 , self). 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 the Internet" def items (self): return articlE.objects.filter (Hided=false, Published=true, Sync_status=sync_status. synced). Order_by ('-publish_date ') [: A] def item_extra_kwargs (self, item): Return {' content_encoded ': self.item_conte Nt_encoded (item)} def item_title (self, item): Return Item.title # Item_link are only needed if NewsItem has no get_
Absolute_url method. def item_link (self, item): Return '/article/%s/'% item.slug def item_description (self, item): Return Item.descript Ion Def item_author_name (self, item): Return Item.creator.get_full_name () def item_pubdate (self, item): Return I
Tem.publish_date def item_content_encoded (self, item): Return item.content
2, URL configuration
From Django Import VERSION
if Version[0:2] > (1, 3):
django.conf.urls Import patterns, include, url
E LSE: From
django.conf.urls.defaults import patterns, include, URLs from
. Feeds Import latestarticlefeed
Urlpatterns = Patterns (
',
url (r ' ^feed/$ ', Latestarticlefeed ())