欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2015年04月08日 16:36:02   作者:吳文苑  
這篇文章主要介紹了給Python的Django框架下搭建的BLOG添加RSS功能的教程,示例代碼非常簡(jiǎn)單,需要的朋友可以參考下

前些天有位網(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方法

    下面小編就為大家分享一篇python中將一個(gè)全部為int的list 轉(zhuǎn)化為str的list方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • Python實(shí)現(xiàn)標(biāo)記數(shù)組的連通域

    Python實(shí)現(xiàn)標(biāo)記數(shù)組的連通域

    這篇文章主要為大家詳細(xì)介紹了如何通過(guò)Python實(shí)現(xiàn)標(biāo)記數(shù)組的連通域,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定的幫助,需要的可以參考一下
    2023-04-04
  • 帶你了解Python妙開(kāi)根號(hào)的三種方式

    帶你了解Python妙開(kāi)根號(hào)的三種方式

    這篇文章主要為大家介紹了Python妙開(kāi)根號(hào)的三種方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-01-01
  • pytorch tensor計(jì)算三通道均值方式

    pytorch tensor計(jì)算三通道均值方式

    這篇文章主要介紹了pytorch tensor計(jì)算三通道均值方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • 詳解python中的Turtle函數(shù)庫(kù)

    詳解python中的Turtle函數(shù)庫(kù)

    這篇文章主要介紹了python中的Turtle函數(shù)庫(kù),包括函數(shù)庫(kù)的引用方式,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-11-11
  • Python擴(kuò)展內(nèi)置類(lèi)型詳解

    Python擴(kuò)展內(nèi)置類(lèi)型詳解

    這篇文章主要為大家詳細(xì)介紹了Python擴(kuò)展內(nèi)置類(lèi)型的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • python安裝本地whl的實(shí)例步驟

    python安裝本地whl的實(shí)例步驟

    在本篇文章里小編給大家整理的是關(guān)于python安裝本地whl的實(shí)例步驟,有需要的朋友們可以學(xué)習(xí)下。
    2019-10-10
  • Keras自定義實(shí)現(xiàn)帶masking的meanpooling層方式

    Keras自定義實(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立方體的方法

    這篇文章主要介紹了使用Python和Scribus創(chuàng)建一個(gè)RGB立方體的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • 詳解python做UI界面的方法

    詳解python做UI界面的方法

    在本文里我們給大家整理了關(guān)于python做UI界面的方法和具體步驟,對(duì)此有需要的朋友們可以跟著學(xué)習(xí)參考下。
    2019-02-02

最新評(píng)論