给Python的Django框架下搭建的BLOG添加RSS功能的教程


Posted in Python onApril 08, 2015

前些天有位网友建议我在博客中添加RSS订阅功能,觉得挺好,所以自己抽空看了一下如何在Django中添加RSS功能,发现使用Django中的syndication feed framework很容易实现。

    具体实现步骤和代码如下:

    1、Feed类

# -*- coding: utf-8 -*-
from django.conf import settings
from django.contrib.syndication.views import Feed
from django.utils.feedgenerator import Rss201rev2Feed
 
from blog.models import Article
from .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://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"关注python、django、vim、linux、web开发和互联网"
 
 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配置

from django import VERSION
 
if VERSION[0: 2] > (1, 3):
 from django.conf.urls import patterns, include, url
else:
 from django.conf.urls.defaults import patterns, include, url
from .feeds import LatestArticleFeed
 
 
urlpatterns = patterns(
 '',
 url(r'^feed/$', LatestArticleFeed()),
)
Python 相关文章推荐
Python  __getattr__与__setattr__使用方法
Sep 06 Python
wxpython 学习笔记 第一天
Feb 09 Python
Python ORM框架SQLAlchemy学习笔记之映射类使用实例和Session会话介绍
Jun 10 Python
将Python代码打包为jar软件的简单方法
Aug 04 Python
用Python实现KNN分类算法
Dec 22 Python
Python基于多线程实现抓取数据存入数据库的方法
Jun 22 Python
Django 表单模型选择框如何使用分组
May 16 Python
python实现抠图给证件照换背景源码
Aug 20 Python
Python 随机生成测试数据的模块:faker基本使用方法详解
Apr 09 Python
Python matplotlib图例放在外侧保存时显示不完整问题解决
Jul 28 Python
Python 整行读取文本方法并去掉readlines换行\n操作
Sep 03 Python
python字符串常规操作大全
May 02 Python
在Python中使用NLTK库实现对词干的提取的教程
Apr 08 #Python
使用Python操作Elasticsearch数据索引的教程
Apr 08 #Python
用Python实现协同过滤的教程
Apr 08 #Python
在Python中调用ggplot的三种方法
Apr 08 #Python
Python字符串和文件操作常用函数分析
Apr 08 #Python
Python遍历zip文件输出名称时出现乱码问题的解决方法
Apr 08 #Python
python smtplib模块发送SSL/TLS安全邮件实例
Apr 08 #Python
You might like
十天学会php之第六天
2006/10/09 PHP
在windows平台上构建自己的PHP实现方法(仅适用于php5.2)
2013/07/05 PHP
php生成txt文件标题及内容的方法
2014/01/16 PHP
PHP执行SQL文件并将SQL文件导入到数据库
2015/09/17 PHP
JavaScript接口实现代码 (Interfaces In JavaScript)
2010/06/11 Javascript
用显卡加速,轻松把笔记本打造成取暖器的办法!
2013/04/17 Javascript
JS简单的图片放大缩小的两种方法
2013/11/11 Javascript
浅析javascript中function 的 length 属性
2014/05/27 Javascript
jQuery实现简洁的导航菜单效果
2015/11/23 Javascript
jQuery筛选数组之grep、each、inArray、map的用法及遍历json对象
2016/06/20 Javascript
jQuery过滤选择器用法示例
2016/09/12 Javascript
微信小程序使用第三方库Underscore.js步骤详解
2016/09/27 Javascript
JS文件/图片从电脑里面拖拽到浏览器上传文件/图片
2017/03/08 Javascript
第一个Vue插件从封装到发布
2017/11/22 Javascript
JS数组方法concat()用法实例分析
2020/01/18 Javascript
JavaScript异步操作的几种常见处理方法实例总结
2020/05/11 Javascript
jQuery 动态粒子效果示例代码
2020/07/07 jQuery
Vue中component标签解决项目组件化操作
2020/09/04 Javascript
JS+CSS实现动态时钟
2021/02/19 Javascript
Python使用wget实现下载网络文件功能示例
2018/05/31 Python
Python函数返回不定数量的值方法
2019/01/22 Python
pyqt 实现QlineEdit 输入密码显示成圆点的方法
2019/06/24 Python
Python 字符串、列表、元组的截取与切片操作示例
2019/09/17 Python
Python实现手机号自动判断男女性别(实例解析)
2019/12/22 Python
Python定时任务框架APScheduler原理及常用代码
2020/10/05 Python
用CSS禁用输入法(CSS3 UI规范)实例解析
2012/12/04 HTML / CSS
html通过canvas转成base64的方法
2019/07/18 HTML / CSS
Space NK英国站:英国热门美妆网站
2017/12/11 全球购物
墨西哥购物网站:Elektra
2020/01/21 全球购物
自我鉴定范文200字
2013/10/02 职场文书
会计电算化专业毕业生求职信范文
2013/12/10 职场文书
公司员工活动策划方案
2014/08/20 职场文书
父亲节寄语大全
2015/02/27 职场文书
读《教育心理学》心得体会
2016/01/22 职场文书
幼儿体育课教学反思
2016/02/16 职场文书
前端与RabbitMQ实时消息推送未读消息小红点实现示例
2022/07/23 Java/Android