在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Python/ Django RSS
Django Session會(huì)話
Django創(chuàng)建視圖
Django教程
Django表單處理
Django創(chuàng)建工程
Django Cookies處理
Django快速入門-表單
Django管理員界面
Django快速入門
Django通用視圖
Django緩存
Django Apache配置
Django發(fā)送E-mail
Django模板系統(tǒng)
Django模型
Django基礎(chǔ)
Django RSS
Django Ajax應(yīng)用
Django快速入門-視圖
Django上傳文件
Django頁面重定向
Django開發(fā)環(huán)境安裝配置
Django快速入門-數(shù)據(jù)庫模型
Django URL映射
Django生命周期

Django RSS

Django帶有聚合feed生成框架。有了它,你可以創(chuàng)建RSS或Atom只需繼承django.contrib.syndication.views.Feed類。

讓我們創(chuàng)建一個(gè)訂閱源的應(yīng)用程序。

from django.contrib.syndication.views import Feed
from django.contrib.comments import Comment
from django.core.urlresolvers import reverse

class DreamrealCommentsFeed(Feed):
   title = "Dreamreal's comments"
   link = "/drcomments/"
   description = "Updates on new comments on Dreamreal entry."

   def items(self):
      return Comment.objects.all().order_by("-submit_date")[:5]
		
   def item_title(self, item):
      return item.user_name
		
   def item_description(self, item):
      return item.comment
		
   def item_link(self, item):
      return reverse('comment', kwargs = {'object_pk':item.pk}) 
  • 在feed類, title, link 和 description 屬性對(duì)應(yīng)標(biāo)準(zhǔn)RSS 的<title>, <link> 和 <description>元素。

  • 條目方法返回應(yīng)該進(jìn)入feed的item的元素。在我們的示例中是最后五個(gè)注釋。

現(xiàn)在,我們有feed,并添加評(píng)論在視圖views.py,以顯示我們的評(píng)論?

from django.contrib.comments import Comment

def comment(request, object_pk):
   mycomment = Comment.objects.get(object_pk = object_pk)
   text = '<strong>User :</strong> %s <p>'%mycomment.user_name</p>
   text += '<strong>Comment :</strong> %s <p>'%mycomment.comment</p>
   return HttpResponse(text) 

我們還需要一些網(wǎng)址在myapp urls.py中映射 ?

from myapp.feeds import DreamrealCommentsFeed
from django.conf.urls import patterns, url

urlpatterns += patterns('',
   url(r'^latest/comments/', DreamrealCommentsFeed()),
   url(r'^comment/(?P\w+)/', 'comment', name = 'comment'),
) 

當(dāng)訪問/myapp/latest/comments/會(huì)得到 feed ?

當(dāng)點(diǎn)擊其中的一個(gè)用戶名都會(huì)得到:/myapp/comment/comment_id 在您的評(píng)論視圖定義之前,會(huì)得到 ?

因此,定義一個(gè)RSS源是 Feed 類的子類,并確保這些URL(一個(gè)用于訪問feed,一個(gè)用于訪問feed元素)的定義。 正如評(píng)論,這可以連接到您的應(yīng)用程序的任何模型。