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

Source: Internet
Author: User
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 ()),)
  • 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.