📜  Django-RSS

📅  最后修改于: 2020-10-25 10:05:35             🧑  作者: Mango


Django带有一个联合供稿生成框架。使用它,您可以仅通过将django.contrib.syndication.views.Feed类类化来创建RSS或Atom提要。

让我们为在该应用程序上完成的最新评论创建一个提要(另请参见Django-Comments Framework一章)。为此,让我们创建一个myapp / feeds.py并定义我们的提要(您可以将提要类放在代码结构中的任何位置)。

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})
  • 在我们的提要类中, titlelinkdescription属性对应于标准RSS </b> , <b><link></b>和<b><description></b>元素。</p> </li> <li> <p> <b>items</b>方法,返回应作为元素添加到提要中的元素。在我们的案例中,最后五个评论。</p> </li> <li> <p> <b>item_title</b>方法将获取供稿项目的标题。在本例中,标题将是用户名。</p> </li> <li> <p> <b>item_description</b>方法将获得对我们的提要项目的描述。就我们而言,评论本身。</p> </li> <li> <p> <b>item_link</b>方法将建立指向完整项目的链接。在我们的情况下,它将使您发表评论。</p> </li> </ul> <p>现在我们有了feed,让我们在views.py中添加评论视图以显示我们的评论-</p> <div class="hcb_wrap"> <pre class="prism line-numbers lang-java" data-lang="java"><code class="language-markup">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) </code></pre> </div> <p>我们还需要myapp urls.py中的一些URL进行映射-</p> <div class="hcb_wrap"> <pre class="prism line-numbers lang-java" data-lang="java"><code class="language-markup">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'), ) </code></pre> </div> <p>访问/ myapp / latest / comments /时,您将获得我们的feed-</p> <p><img alt="Django RSS示例" class="img-fluid" src="https://mangdo-1254073825.cos.ap-chengdu.myqcloud.com//front_eng_imgs/programiz/5ec17068dea276a955a4da4ac7d54a4cf3d39273780189268ab46341_22.jpg"/></p> <p>然后单击其中一个用户名将使您转到:/ myapp / comment / comment_id(之前在我们的评论视图中定义),您将获得-</p> <p><img alt="Django RSS重定向页面" class="img-fluid" src="https://mangdo-1254073825.cos.ap-chengdu.myqcloud.com//front_eng_imgs/programiz/5ec17068dea276a955a4da4ac7d54a4cf3d39273780189268ab46341_23.jpg"/></p> <p>因此,定义RSS feed只是对Feed类进行子类化,并确保定义了URL(一个用于访问该feed和一个用于访问feed元素)的问题。就像评论一样,它可以附加到您应用中的任何模型。</p> </div> </div> </div> </div> </div> </div> <footer> <div class="bg-white text-center pb-1"> <p class="text-body-tertiary pt-3 lh-lg text-opacity-50" id="footer-text">Copyright © 2020 - 2024 版权所有 <br> <a href="https://beian.miit.gov.cn/" target="_blank" class="text-opacity-50 text-body-tertiary mt-1 mb-1">蜀ICP备20006366号-1</a> <br> Made with ❤️ in Chengdu </p> </div> </footer> <!-- 引入Bootstrap JavaScript库 --> <script src="https://unpkg.com/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script> <!-- 引入Meilisearch搜索相关的JavaScript库 --> <script src="https://cdn.jsdelivr.net/npm/@meilisearch/instant-meilisearch/dist/instant-meilisearch.umd.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/instantsearch.js@4"></script> <script src="https://imangodoc.com/static/javascript/meili_custom.js"></script> <!-- 引入prismjs代码高亮相关的JavaScript库 --> <script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/components/prism-core.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/toolbar/prism-toolbar.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/autoloader/prism-autoloader.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/copy-to-clipboard/prism-copy-to-clipboard.min.js"></script> </body> </html>