黑松山资源网 Design By www.paidiu.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
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()), )
黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com
暂无评论...
更新日志
2024年10月09日
2024年10月09日
- 《原神》「星路拾忆」网页活动壁纸展示
- 群星.2024-祝你幸福!电影原声带【阿里巴巴】【FLAC分轨】
- 于台烟.2004-执着【乾坤唱片】【WAV+CUE】
- 黄品源.2004-感谢·情人(引进版)【滚石】【WAV+CUE】
- 《最终幻想16》评测:游、戏两难全的系列转折
- 《纸嫁衣》五部打包¥104,《鬼泣5》仅需¥44
- 《崩坏:星穹铁道》造物大赛卷出新高度
- 《王菲 菲常传奇 2CD》[WAV+CUE][950MB]
- 《王菲 空灵独味唱腔 非常传奇 紫水晶2CD》[WAV+CUE][860MB]
- 《车载专业测试王1号 极品音质》[WAV/分轨][980MB]
- 炉石传说的国服回归送什么奖励 炉石传说9.25回归最新消息介绍
- 云顶之弈最强阵容汇总2024 云顶之弈最强阵容搭配最新版本
- S14lol全球总决赛在哪观看 lol全球总决赛观赛直播地址汇总
- 黄思婷2011-感谢天[豪记唱片][WAV+CUE]
- 孙淑媚2013-太阳[台湾首版][WAV+CUE]