给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 正则表达式 概述及常用字符
May 04 Python
Python实现读取TXT文件数据并存进内置数据库SQLite3的方法
Aug 08 Python
Python从文件中读取指定的行以及在文件指定位置写入
Sep 06 Python
matplotlib绘制多个子图(subplot)的方法
Dec 03 Python
python中with用法讲解
Feb 07 Python
通过python连接Linux命令行代码实例
Feb 18 Python
python使用html2text库实现从HTML转markdown的方法详解
Feb 21 Python
Python爬取365好书中小说代码实例
Feb 28 Python
Django框架静态文件处理、中间件、上传文件操作实例详解
Feb 29 Python
python如何实现图片压缩
Sep 11 Python
python requests模块的使用示例
Apr 07 Python
Python+Selenium实现抖音、快手、B站、小红书、微视、百度好看视频、西瓜视频、微信视频号、搜狐视频、一点号、大风号、趣头条等短视频自动发布
Apr 13 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中使用DOM类读取XML文件的实现代码
2011/12/14 PHP
Php中用PDO查询Mysql来避免SQL注入风险的方法
2013/04/25 PHP
discuz图片顺序混乱解决方案
2015/07/29 PHP
PHP+apc+ajax实现的ajax_upload上传进度条代码
2016/01/25 PHP
php递归函数怎么用才有效
2018/02/24 PHP
PHP7.3.10编译安装教程
2019/10/08 PHP
JQuery中each()的使用方法说明
2010/08/19 Javascript
js 动态修改css文件用到了cssRule
2014/08/20 Javascript
Javascript保存网页为图片借助于html2canvas库实现
2014/09/05 Javascript
使用javaScript动态加载Js文件和Css文件
2015/10/24 Javascript
Bootstrap入门书籍之(四)菜单、按钮及导航
2016/02/17 Javascript
jquery 抽奖小程序实现代码
2016/10/12 Javascript
网站申请不到支付宝接口、微信接口,免接口收款实现方式几种解决办法
2016/12/14 Javascript
Angular.js与node.js项目里用cookie校验账户登录详解
2017/02/22 Javascript
vue项目实现记住密码到cookie功能示例(附源码)
2018/01/31 Javascript
fullpage.js最后一屏滚动方式
2018/02/06 Javascript
nodejs读取并去重excel文件
2018/04/22 NodeJs
关于vue-router的那些事儿
2018/05/23 Javascript
详解React 的几种条件渲染以及选择
2018/10/23 Javascript
vue使用原生js实现滚动页面跟踪导航高亮的示例代码
2018/10/25 Javascript
js实现上下左右键盘控制div移动
2020/01/16 Javascript
[01:38]完美世界高校联赛决赛花絮
2018/12/02 DOTA
Python函数嵌套实例
2014/09/23 Python
python进程类subprocess的一些操作方法例子
2014/11/22 Python
Python自定义scrapy中间模块避免重复采集的方法
2015/04/07 Python
Python实现将一个大文件按段落分隔为多个小文件的简单操作方法
2017/04/17 Python
几种实用的pythonic语法实例代码
2018/02/24 Python
将字典转换为DataFrame并进行频次统计的方法
2018/04/08 Python
CSS3毛玻璃效果(blur)有白边问题的解决方法
2016/11/15 HTML / CSS
英国最受欢迎的平价女士时装零售商:Roman Originals
2019/11/02 全球购物
巾帼文明岗汇报材料
2014/12/24 职场文书
全国爱眼日活动总结
2015/02/27 职场文书
2015年行政助理工作总结
2015/04/30 职场文书
用Python爬虫破解滑动验证码的案例解析
2021/05/06 Python
IDEA使用SpringAssistant插件创建SpringCloud项目
2021/06/23 Java/Android
python识别围棋定位棋盘位置
2021/07/26 Python