给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使用wmi模块获取windows下硬盘信息的方法
May 15 Python
在Python中操作字典之setdefault()方法的使用
May 21 Python
使用PyInstaller将Python程序文件转换为可执行程序文件
Jul 08 Python
Python基于identicon库创建类似Github上用的头像功能
Sep 25 Python
Python2.X/Python3.X中urllib库区别讲解
Dec 19 Python
Python动刷新抢12306火车票的代码(附源码)
Jan 24 Python
浅谈pytorch、cuda、python的版本对齐问题
Jan 15 Python
Python tcp传输代码实例解析
Mar 18 Python
详解python如何引用包package
Jun 07 Python
记一次python 爬虫爬取深圳租房信息的过程及遇到的问题
Nov 24 Python
基于Python的接口自动化读写excel文件的方法
Jan 15 Python
详解Python生成器和基于生成器的协程
Jun 03 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
ThinkPHP3.1新特性之对Ajax的支持更加完善
2014/06/19 PHP
从零开始学YII2框架(二)通过 Composer 安装扩展插件
2014/08/20 PHP
Git命令之分支详解
2021/03/02 PHP
extjs实现选择多表自定义查询功能 前台部分(ext源码)
2011/12/20 Javascript
JS打印gridview实现原理及代码
2013/02/05 Javascript
javascript中的void运算符语法及使用介绍
2013/03/10 Javascript
jQuery学习笔记(2)--用jquery实现各种模态提示框代码及项目构架
2013/04/08 Javascript
细说javascript函数从函数的构成开始
2013/08/29 Javascript
js判断登录与否并确定跳转页面的方法
2015/01/30 Javascript
Javascript核心读书有感之表达式和运算符
2015/02/11 Javascript
JavaScript实现文字跟随鼠标特效
2015/08/06 Javascript
js格式化输入框内金额、银行卡号
2016/02/01 Javascript
基于jquery实现轮播特效
2016/04/22 Javascript
jQuery实现边框动态效果的实例代码
2016/09/23 Javascript
EasyUI 结合JS导出Excel文件的实现方法
2016/11/10 Javascript
js实现简易垂直滚动条
2017/02/22 Javascript
jQuery插件echarts去掉垂直网格线用法示例
2017/03/03 Javascript
详解微信开发中snsapi_base和snsapi_userinfo及静默授权的实现
2017/03/11 Javascript
Vue中img的src属性绑定与static文件夹实例
2017/05/18 Javascript
nodejs中express入门和基础知识点学习
2018/09/13 NodeJs
JS函数本身的作用域实例分析
2020/03/16 Javascript
Python日志模块logging简介
2015/04/13 Python
python递归计算N!的方法
2015/05/05 Python
Python实现针对给定字符串寻找最长非重复子串的方法
2018/04/21 Python
Python实现的计算器功能示例
2018/04/26 Python
Python subprocess模块功能与常见用法实例详解
2018/06/28 Python
python实现控制台打印的方法
2019/01/12 Python
Python DataFrame一列拆成多列以及一行拆成多行
2019/08/06 Python
Python中if有多个条件处理方法
2020/02/26 Python
详解pyinstaller生成exe的闪退问题解决方案
2020/06/19 Python
python爬取youtube视频的示例代码
2021/03/03 Python
HMV日本官网:全球知名的音乐、DVD和电脑游戏零售巨头
2016/08/13 全球购物
澳大利亚电商Catch新西兰站:Catch.co.nz
2020/05/30 全球购物
运动会广播稿200字
2014/01/15 职场文书
本科生职业生涯规划书范文
2014/01/21 职场文书
幼儿园学前班幼儿评语
2014/12/29 职场文书