• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > 给Python的Django框架下搭建的BLOG添加RSS功能的教程

给Python的Django框架下搭建的BLOG添加RSS功能的教程

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了django blog,python安装django,python.django,python django教程,python web django等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

前些天有位网友建议我在博客中添加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
</div>

    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()),
)
</div>

</div>

您可能想查找下面的文章:

  • 给Python的Django框架下搭建的BLOG添加RSS功能的教程
  • 给Python的Django框架下搭建的BLOG添加RSS功能的教程

相关文章

  • Python标准库之sqlite3使用实例
  • Python自定义类的数组排序实现代码
  • python登录豆瓣并发帖的方法
  • Python中文件I/O高效操作处理的技巧分享
  • python实现二维码扫码自动登录淘宝
  • 简单使用Python自动生成文章
  • python中xrange和range的区别
  • python实现bitmap数据结构详解
  • python连接mysql数据库示例(做增删改操作)
  • Django与遗留的数据库整合的方法指南

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 使用python3.5仿微软记事本notepad
    • python 读写、创建 文件的方法(必看)
    • python链接Oracle数据库的方法
    • 利用python获取某年中每个月的第一天和最后一天
    • Python易忽视知识点小结
    • Python3搜索及替换文件中文本的方法
    • Python实现网站文件的全备份和差异备份
    • Python全局变量操作详解
    • python每隔N秒运行指定函数的方法
    • Python缩进和冒号详解

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有