django rss -九游会下载
django带有聚合feed生成框架。有了它,你可以创建rss或atom只需继承django.contrib.syndication.views.feed类。
让我们创建一个订阅源的应用程序。
# filename : example.py
# 九游会下载 copyright : 2020 by nhooo
# author by : https://www.elefans.com/biancheng/
# 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 = "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 属性对应标准rss 的
, <link> 和 <description>元素。</p><p><span class="line">
<p class="desc">条目方法返回应该进入feed的item的元素。在我们的示例中是最后五个注释。</p></span> </p><p class="desc">现在,我们有feed,并添加评论在视图views.py,以显示我们的评论−</p><div class="example_code"><label class="example_title">示例</label><pre># filename : example.py
# 九游会下载 copyright : 2020 by nhooo
# author by : https://www.elefans.com/biancheng/
# 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)</pre></div><p class="desc">我们还需要一些网址在myapp urls.py中映射 −</p><div class="example_code"><label class="example_title">示例</label><pre># filename : example.py
# 九游会下载 copyright : 2020 by nhooo
# author by : https://www.elefans.com/biancheng/
# date : 2020-08-08
from myapp.feeds import dreamrealcommentsfeed
from django.conf.urls import patterns, url
urlpatterns = patterns('',
),
/', 'comment', name = 'comment'),
)</pre></div><p class="desc">当访问/myapp/latest/comments/会得到 feed −
<img src="../static/upload/210427/0542020.jpg" alt="feed"> </p><p class="desc">当点击其中的一个用户名都会得到:/myapp/comment/comment_id 在您的评论视图定义之前,会得到 −
<img src="../static/upload/210427/0542021.jpg" alt="feed"> </p><p class="desc">因此,定义一个rss源是 feed 类的子类,并确保这些的定义。 正如评论,这可以连接到您的应用程序的任何模型。</p><div class="col-md-12 mb10 bg-ef"><a class="fr pre-xs-show-right" href="django-ajax.html">django ajax应用</a><a class="fl pre-xs-show-left" href="django-caching.html">django 缓存</a></div>
</div>
<div class="col-md-2 col-md-right">
<div class="panel panel-default">
</div>
<div class="panel panel-default">
</div>
</div>
</div>
</div>
<script src="../static/script/bootstrap.min.js" type="text/javascript"></script>
<link rel="stylesheet" href="../static/script/hljs/styles/vs.css"><script src="../static/script/hljs/highlight.pack.js"></script>
<div class="container"><div class="row"><div class="col-sm-12"><p>
<a href="https://www.elefans.com/biancheng/" target="_blank">编程</a></p></div></div></div>
<div class="九游会下载 copyright" id="footer"><div class="container"><div class="row"><div class="col-sm-12"><span>九游会下载 copyright ©2022 <a href="../index.html">基础教程</a> www.elefans.com</span></div></div></div></div><a class="gotop" href="#top" title="返回顶部" style="display: none;"><span class="arrow"></span><span class="arrow lit"></span></a>
<a class="gotop" href="#top" title="返回顶部" style="display: none;"><span class="arrow"></span><span class="arrow lit"></span></a></body></html>