Tutorial on adding RSS to a BLOG built under the Django framework of Python, djangorss

Source: Internet
Author: User

Tutorial on adding RSS to a BLOG built under the Django framework of Python, djangorss

A netizen suggested that I add the RSS subscription function to my blog a few days ago, so I took the time to read how to add the RSS function to Django, it is found that using the syndication feed framework in Django is easy to implement.

The 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 ExtendedRSSFeed (Rss201rev2Feed): mime_type = 'application/xml' "Create a type of RSS feed that has content: encoded elements. "def root_attributes (self): attrs = super (ExtendedRSSFeed, self ). root_attributes () attrs ['xmlns: content'] = 'HTTP: // your 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 "follow 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') [: 10] def item_extra_kwargs (self, item): return {'content _ encoded': self. item_content_encoded (item)} def item_title (self, item): return item. title # item_link is 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. description def item_author_name (self, item): return item. creator. get_full_name () def item_pubdate (self, item): return item. publish_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 django.conf.urls.defaults import patterns, include, urlfrom .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.