给Python的Django框架下搭建的BLOG添加RSS功能的教程
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了给Python的Django框架下搭建的BLOG添加RSS功能的教程,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2065字,纯文字阅读大概需要3分钟。
内容图文
![给Python的Django框架下搭建的BLOG添加RSS功能的教程](/upload/InfoBanner/zyjiaocheng/437/460ec627f72e4e22b38c6a63cc810359.jpg)
具体实现步骤和代码如下:
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的Django框架下搭建的BLOG添加RSS功能的教程全部内容,希望文章能够帮你解决给Python的Django框架下搭建的BLOG添加RSS功能的教程所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。