Django添加feeds功能的示例
概念:RSS和Atom都是基于XML的格式,你可以用它來提供有關(guān)你站點(diǎn)內(nèi)容的自動(dòng)更新的feed。了解更多關(guān)于RSS的可以訪問 http://www.whatisrss.com/ , 更多Atom的信息可以訪問 http://www.atomenabled.org/ .
RSS(簡(jiǎn)易信息聚合)是一種消息來源格式規(guī)范,用以聚合經(jīng)常發(fā)布更新數(shù)據(jù)的網(wǎng)站,例如博客文章、新聞、音頻或視頻的網(wǎng)摘。RSS文件(或稱做摘要、網(wǎng)絡(luò)摘要、或頻更新,提供到頻道)包含全文或是節(jié)錄的文字,再加上發(fā)布者所訂閱之網(wǎng)摘數(shù)據(jù)和授權(quán)的元數(shù)據(jù)。
其實(shí)就是一種聚合閱讀,這樣可以用feedly等工具來訂閱你喜歡的網(wǎng)站,這樣他們的網(wǎng)站更新了之后你就可以通過feedly這種工具來閱讀更新的內(nèi)容,而不用跑到網(wǎng)站上面去查看。
下面介紹下如何在你的Django網(wǎng)站中添加RSS功能,其實(shí)很簡(jiǎn)單:
1、首先建立一個(gè)Feed類,這個(gè)Feed類提供了源所需要的數(shù)據(jù):title,link,description
示例代碼如下: 創(chuàng)建feed.py:
from django.contrib.syndication.views import Feed
from blog.models import Article
from django.conf import settings
from django.utils.feedgenerator import Rss201rev2Feed
from DjangoBlog.common_markdown import common_markdown
from django.contrib.auth import get_user_model
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
class DjangoBlogFeed(Feed):
feed_type = Rss201rev2Feed
description = settings.SITE_DESCRIPTION
feed_url = 'https://www.fkomm.cn/feed'
title = "%s %s " % (settings.SITE_NAME, settings.SITE_DESCRIPTION)
link = "https://www.fkomm.cn"
def author_name(self):
return get_user_model().objects.first().nickname
def author_link(self):
return get_user_model().objects.first().get_absolute_url()
def items(self):
return Article.objects.order_by('-pk')[:5]
def item_title(self, item):
return item.title
def item_description(self, item):
return common_markdown.get_markdown(item.body)
def feed_copyright(self):
# print(Site.objects.get_current().name)
return "Copyright© 2018 " + settings.SITE_NAME
def item_link(self, item):
return item.get_absolute_url()
def item_guid(self, item):
return
2、然后在urls.py中添加:
from DjangoBlog.feeds import DjangoBlogFeed urlpatterns = [ ...... url(r'^feed/$',DjangoBlogFeed()), ]
至此,全部完成,可以打開
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于DataFrame數(shù)據(jù)的查詢和編輯
在使用pandas處理DataFrame時(shí),可通過列索引標(biāo)簽獲取列數(shù)據(jù),行數(shù)據(jù)的獲取可以利用行索引或位置切片,如iloc和loc方法,增加數(shù)據(jù)時(shí),可通過append方法增加行,直接賦值增加列,刪除數(shù)據(jù)則通過drop方法,通過設(shè)置axis參數(shù)確定是刪除行還是列2024-09-09
numpy 實(shí)現(xiàn)返回指定行的指定元素的位置索引
這篇文章主要介紹了numpy 實(shí)現(xiàn)返回指定行的指定元素的位置索引操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-05-05
python經(jīng)典練習(xí)百題之猴子吃桃三種解法
這篇文章主要給大家介紹了關(guān)于python經(jīng)典練習(xí)百題之猴子吃桃三種解法的相關(guān)資料, Python猴子吃桃子編程是一個(gè)趣味性十足的編程練習(xí),在這個(gè)練習(xí)中,我們將要使用Python語(yǔ)言來模擬一只猴子吃桃子的過程,需要的朋友可以參考下2023-10-10
Linux系統(tǒng)中設(shè)置Python程序開機(jī)啟動(dòng)的兩種方式
在 Linux 系統(tǒng)中設(shè)置Python 腳本開機(jī)啟動(dòng),通??梢酝ㄟ^以下幾種方式實(shí)現(xiàn), 使用 systemd(推薦方式)和使用 crontab(對(duì)于簡(jiǎn)單任務(wù)),文章通過代碼示例給大家講解的非常詳細(xì),需要的朋友可以參考下2024-05-05
Python用棧實(shí)現(xiàn)隊(duì)列的基本操作
隊(duì)列(Queue)和棧(Stack)是常見的數(shù)據(jù)結(jié)構(gòu),它們?cè)谟?jì)算機(jī)科學(xué)中有著廣泛的應(yīng)用,在Python中,可以使用列表(List)來實(shí)現(xiàn)棧,但要用棧來實(shí)現(xiàn)隊(duì)列需要一些巧妙的操作,本文就給大家詳細(xì)介紹一下Python中如何用棧實(shí)現(xiàn)隊(duì)列,需要的朋友可以參考下2023-11-11

