A tutorial on adding RSS to a blog built under the Python Django framework _python

Source: Internet
Author: User

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 ())

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.