给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中__call__方法示例分析
Oct 11 Python
Python多线程编程(七):使用Condition实现复杂同步
Apr 05 Python
使用Python编写一个在Linux下实现截图分享的脚本的教程
Apr 24 Python
详细解读Python中的__init__()方法
May 02 Python
批处理与python代码混合编程的方法
May 19 Python
基于pandas将类别属性转化为数值属性的方法
Jul 25 Python
使用Django2快速开发Web项目的详细步骤
Jan 06 Python
python函数修饰符@的使用方法解析
Sep 02 Python
Python 读取 YUV(NV12) 视频文件实例
Dec 09 Python
python如何实现不可变字典inmutabledict
Jan 08 Python
Python递归及尾递归优化操作实例分析
Feb 01 Python
python对文件的操作方法汇总
Feb 28 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
搜索引擎技术核心揭密
2006/10/09 PHP
浅析php插件 Simple HTML DOM 用DOM方式处理HTML
2013/07/01 PHP
php四种基础算法代码实例
2013/10/29 PHP
PHP 常用的header头部定义汇总
2015/06/19 PHP
ThinkPHP框架搭建及常见问题(XAMPP安装失败、Apache/MySQL启动失败)
2016/04/15 PHP
PHP面向对象程序设计之对象克隆clone和魔术方法__clone()用法分析
2019/06/12 PHP
JavaScript实现的GBK、UTF8字符串实际长度计算函数
2014/08/27 Javascript
解析JavaScript的ES6版本中的解构赋值
2015/07/28 Javascript
JS实现的N多简单无缝滚动代码(包含图文效果)
2015/11/06 Javascript
jQuery使用$.ajax进行即时验证的方法
2015/12/08 Javascript
js + css实现标签内容切换功能(实例讲解)
2017/10/09 Javascript
node.js中fs文件系统目录操作与文件信息操作
2018/02/24 Javascript
Koa2微信公众号开发之消息管理
2018/05/16 Javascript
jQuery实现模糊搜索功能的方法分析
2018/06/29 jQuery
详解vue项目接入微信JSSDK的坑
2018/12/14 Javascript
探索JavaScript中私有成员的相关知识
2019/06/13 Javascript
React组件设计模式之组合组件应用实例分析
2020/04/29 Javascript
JS图片懒加载技术实现过程解析
2020/07/27 Javascript
ES6中的Javascript解构的实现
2020/10/30 Javascript
python正则匹配查询港澳通行证办理进度示例分享
2013/12/27 Python
Python 爬虫模拟登陆知乎
2016/09/23 Python
Python如何为图片添加水印
2016/11/25 Python
windows10下python3.5 pip3安装图文教程
2018/04/02 Python
Python实现决策树C4.5算法的示例
2018/05/30 Python
转换科学计数法的数值字符串为decimal类型的方法
2018/07/16 Python
详解通过API管理或定制开发ECS实例
2018/09/30 Python
python读取并定位excel数据坐标系详解
2019/06/26 Python
python多线程扫描端口(线程池)
2019/09/04 Python
Python2 与Python3的版本区别实例分析
2020/03/30 Python
python上selenium的弹框操作实现
2020/07/13 Python
趣味比赛活动方案
2014/02/15 职场文书
2014年学习雷锋活动总结
2014/03/01 职场文书
2016年小学教师师德承诺书
2016/03/25 职场文书
Python深度学习之实现卷积神经网络
2021/06/05 Python
聊一聊Redis与MySQL双写一致性如何保证
2021/06/26 Redis
python创建字典及相关管理操作
2022/04/13 Python