English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Django는 합성 피드 생성 프레임워크를 가지고 있습니다. 이를 통해 django.contrib.syndication.views.Feed 클래스를 상속하여 RSS나 Atom을 생성할 수 있습니다.
구독 소스 애플리케이션을 생성하겠습니다.
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : ko.oldtoolbag.com # Date : 2020-08-08 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 = "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 속성은 표준 RSS의 <title>, <link>, <description> 요소와 일치합니다.
entry_method는 피드에 들어갈 item의 요소를 반환해야 합니다. 예제에서는 마지막 다섯 개의 주석입니다.
현재, 우리는 feed를 가지고 있으며, views.py에 의해 추가된 주석을 표시하기 위해 뷰를 추가했습니다.
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : ko.oldtoolbag.com # Date : 2020-08-08 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)
myapp urls.py에서 추가적인 URL을 매핑해야 합니다.
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : ko.oldtoolbag.com # Date : 2020-08-08 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'), )
접근할 때/myapp/latest/comments/feed를 받습니다.
특정 사용자 이름을 클릭하면 다음과 같이 됩니다:/myapp/comment/comment_id는 comment 뷰 정의 전에 -를 받습니다.
따라서, RSS 소스를 정의하는 것은 Feed 클래스의 서브클래스이며, 이러한 URL(Feed에 접근하기 위한 하나, Feed 요소에 접근하기 위한 하나) 정의를 보장해야 합니다. 이와 같은 설명에 따르면, 이는应用程序의 어떤 모델에도 연결될 수 있습니다.