给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列表list内建函数用法实例分析【insert、remove、index、pop等】
Jul 24 Python
python使用PyCharm进行远程开发和调试
Nov 02 Python
python实现SOM算法
Feb 23 Python
Python解决两个整数相除只得到整数部分的实例
Nov 10 Python
python 函数中的内置函数及用法详解
Jul 02 Python
Python列表原理与用法详解【创建、元素增加、删除、访问、计数、切片、遍历等】
Oct 30 Python
TensorFlow梯度求解tf.gradients实例
Feb 04 Python
Pycharm及python安装详细步骤及PyCharm配置整理(推荐)
Jul 31 Python
Jupyter notebook 启动闪退问题的解决
Apr 13 Python
Django之全局使用request.user.username的实例详解
May 14 Python
pytorch 两个GPU同时训练的解决方案
Jun 01 Python
基于Python编写一个监控CPU的应用系统
Jun 25 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 版本]
2007/03/20 PHP
简单的PHP留言本实例代码
2010/05/09 PHP
php指定函数参数默认值示例代码
2013/12/04 PHP
PHP读取文件内容后清空文件示例代码
2014/03/18 PHP
php中stdClass的用法分析
2015/02/27 PHP
ThinkPHP静态缓存简单配置和使用方法详解
2016/03/23 PHP
php从身份证获取性别和出生年月
2017/02/09 PHP
PHP简单实现模拟登陆功能示例
2017/09/15 PHP
jQuery学习7 操作JavaScript对象和集合的函数
2010/02/07 Javascript
IE8 chrome中table隔行换色解决办法
2010/07/09 Javascript
IE下JS读取xml文件示例代码
2013/08/05 Javascript
jquery 按钮状态效果 正常、移上、按下
2013/08/12 Javascript
使用jquery.upload.js实现异步上传示例代码
2014/07/29 Javascript
JavaScript判断浏览器类型的方法
2015/02/10 Javascript
纯js实现无限空间大小的本地存储
2015/06/18 Javascript
javascript生成img标签的3种实现方法(对象、方法、html)
2015/12/25 Javascript
node.js中cluster的使用教程
2017/06/09 Javascript
客户端(vue框架)与服务器(koa框架)通信及服务器跨域配置详解
2017/08/26 Javascript
详解Vue 数据更新了但页面没有更新的 7 种情况汇总及延伸总结
2020/05/28 Javascript
OpenLayers3实现对地图的基本操作
2020/09/28 Javascript
[03:58]2014DOTA2国际邀请赛 龙宝赛后解密DK获胜之道
2014/07/14 DOTA
[44:15]DOTA2上海特级锦标赛主赛事日 - 5 败者组决赛Liquid VS EG第二局
2016/03/06 DOTA
利用Python脚本在Nginx和uwsgi上部署MoinMoin的教程
2015/05/05 Python
python魔法方法-属性访问控制详解
2016/07/25 Python
Python回文字符串及回文数字判定功能示例
2018/03/20 Python
关于阿里云oss获取sts凭证 app直传 python的实例
2019/08/20 Python
K最近邻算法(KNN)---sklearn+python实现方式
2020/02/24 Python
pycharm 2018 激活码及破解补丁激活方式
2020/09/21 Python
细节决定成败演讲稿
2014/05/12 职场文书
广播体操口号
2014/06/18 职场文书
中专毕业生的自荐书
2014/07/01 职场文书
简单通用的简历自我评价
2014/09/21 职场文书
初中差生评语
2014/12/29 职场文书
长城的导游词
2015/01/30 职场文书
互联网的下一个风口:新的独角兽将诞生
2019/08/02 职场文书
python树莓派通过队列实现进程交互的程序分析
2021/07/04 Python