给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实现的Kmeans++算法实例
Apr 26 Python
python通过定义一个类实例作为ftp回调方法
May 04 Python
python实现马耳可夫链算法实例分析
May 20 Python
深入理解Django的自定义过滤器
Oct 17 Python
Python代码缩进和测试模块示例详解
May 07 Python
CentOS下Python3的安装及创建虚拟环境的方法
Nov 28 Python
Python正则表达式和元字符详解
Nov 29 Python
keras使用Sequence类调用大规模数据集进行训练的实现
Jun 22 Python
python性能测试工具locust的使用
Dec 28 Python
Python基础之元组与文件知识总结
May 19 Python
Python基础教程,Python入门教程(超详细)
Jun 24 Python
Python 恐龙跑跑小游戏实现流程
Feb 15 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
CodeIgniter读写分离实现方法详解
2016/01/20 PHP
PHP程序员简单的开展服务治理架构操作详解(一)
2020/05/14 PHP
发现的以前不知道的函数
2006/09/19 Javascript
读jQuery之三(构建选择器)
2011/06/11 Javascript
Javascript引用指针使用介绍
2012/11/07 Javascript
js获得鼠标的坐标值的方法
2013/03/13 Javascript
javascript生成img标签的3种实现方法(对象、方法、html)
2015/12/25 Javascript
原生Javascript和jQuery做轮播图简单例子
2016/10/11 Javascript
AngularJS实现动态编译添加到dom中的方法
2016/11/04 Javascript
解决vue-cli中stylus无法使用的问题方法
2017/06/19 Javascript
jquery图片放大镜效果
2017/06/23 jQuery
解决html input验证只能输入数字,不能输入其他的问题
2017/07/21 Javascript
Js中async/await的执行顺序详解
2017/09/22 Javascript
JS实现可视化文件上传
2018/09/08 Javascript
抖音上用记事本编写爱心小程序教程
2019/04/17 Javascript
[14:25]教你分分钟做大人:主宰(HEROS)
2014/12/08 DOTA
Python不规范的日期字符串处理类
2014/06/10 Python
Python基于二分查找实现求整数平方根的方法
2016/05/12 Python
树莓派与PC端在局域网内运用python实现即时通讯
2019/06/22 Python
python如何实现从视频中提取每秒图片
2020/10/22 Python
Python time库基本使用方法分析
2019/12/13 Python
python 自动识别并连接串口的实现
2021/01/19 Python
5分钟实现Canvas鼠标跟随动画背景
2019/11/18 HTML / CSS
泰国办公用品购物网站:OfficeMate
2018/02/04 全球购物
植树节活动总结
2014/04/30 职场文书
入党综合考察材料
2014/06/02 职场文书
施工安全协议书范本
2014/09/26 职场文书
青春雷锋观后感
2015/06/10 职场文书
贷款工资证明范本
2015/06/12 职场文书
小学生纪律委员竞选稿
2015/11/19 职场文书
2016大一新生军训心得体会
2016/01/11 职场文书
2016关于读书活动的心得体会
2016/01/14 职场文书
优质护理服务心得体会
2016/01/22 职场文书
Nginx隐藏式跳转(浏览器URL跳转后保持不变)
2022/04/07 Servers
MySQL事务的ACID特性以及并发问题方案
2022/07/15 MySQL
TypeScript实用技巧 Nominal Typing名义类型详解
2022/09/23 Javascript