給Python的Django框架下搭建的BLOG添加RSS功能的教程
前些天有位網(wǎng)友建議我在博客中添加RSS訂閱功能,覺(jué)得挺好,所以自己抽空看了一下如何在Django中添加RSS功能,發(fā)現(xiàn)使用Django中的syndication feed framework很容易實(shí)現(xiàn)。
具體實(shí)現(xiàn)步驟和代碼如下:
1、Feed類(lèi)
# -*- 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"關(guān)注python、django、vim、linux、web開(kāi)發(fā)和互聯(lián)網(wǎng)" 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()), )
相關(guān)文章
python中將一個(gè)全部為int的list 轉(zhuǎn)化為str的list方法
下面小編就為大家分享一篇python中將一個(gè)全部為int的list 轉(zhuǎn)化為str的list方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04Python實(shí)現(xiàn)標(biāo)記數(shù)組的連通域
這篇文章主要為大家詳細(xì)介紹了如何通過(guò)Python實(shí)現(xiàn)標(biāo)記數(shù)組的連通域,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定的幫助,需要的可以參考一下2023-04-04Keras自定義實(shí)現(xiàn)帶masking的meanpooling層方式
這篇文章主要介紹了Keras自定義實(shí)現(xiàn)帶masking的meanpooling層方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06使用Python和Scribus創(chuàng)建一個(gè)RGB立方體的方法
這篇文章主要介紹了使用Python和Scribus創(chuàng)建一個(gè)RGB立方體的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07