当前位置:K88软件开发文章中心网站服务器框架django → 文章内容

Django 输出非HTML内容

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-25 14:19:24

可以修改 "feeds/" 前缀以满足您自己的要求. )URLConf里有一行参数: {'feed_dict': feeds},这个参数可以把对应URL需要发布的feed内容传递给 syndication framework特别的,feed_dict应该是一个映射feed的slug(简短URL标签)到它的Feed类的字典 你可以在URL配置本身里定义feed_dict,这里是一个完整的例子 You can define the feed_dict in the URLconf itself. Here’s a full example URLconf:from django.conf.urls.defaults import *from mysite.feeds import LatestEntries, LatestEntriesByCategoryfeeds = { 'latest': LatestEntries, 'categories': LatestEntriesByCategory,}urlpatterns = patterns('', # ... (r'^feeds/(?P<url>.*)/$', 'django.contrib.syndication.views.feed', {'feed_dict': feeds}), # ...)前面的例子注册了两个feed:LatestEntries表示的内容将对应到feeds/latest/ .LatestEntriesByCategory的内容将对应到 feeds/categories/ .以上的设定完成之后,接下来需要自己定义 Feed 类一个 Feed 类是一个简单的python类,用来表示一个syndication feed. 一个feed可能是简单的 (例如一个站点新闻feed,或者最基本的,显示一个blog的最新条目),也可能更加复杂(例如一个显示blog某一类别下所有条目的feed。 这里类别 category 是个变量).Feed类必须继承django.contrib.syndication.feeds.Feed,它们可以在你的代码树的任何位置一个简单的FeedThis simple example describes a feed of the latest five blog entries for a given blog:from django.contrib.syndication.feeds import Feedfrom mysite.blog.models import Entryclass LatestEntries(Feed): title = "My Blog" link = "/archive/" description = "The latest news about stuff." def items(self): return Entry.objects.order_by('-pub_date')[:5]要注意的重要的事情如下所示:子类 django.contrib.syndication.feeds.Feed .title , link , 和 description 对应一个标准 RSS 里的  ,  , 和  标签.items() 是一个方法,返回一个用以包含在包含在feed的  元素里的 list 虽然例子里用Djangos database API返回的 NewsItem 对象, items() 不一定必须返回 model的实例 Although this example returns Entry objects using Django’s database API, items() doesn’t have to return model instances.还有一个步骤,在一个RSS feed里,每个(item)有一个(title),(link)和(description),我们需要告诉框架 把数据放到这些元素中 In an RSS feed, each  has a  ,  , and  . We need to tell the framework what data to put into those elements.如果要指定  和  ,可以建立一个Django模板(见Chapter 4)名字叫feeds/latest_title.html 和 feeds/latest_description.html ,后者是URLConf里为对应feed指定的slug 。注意 .html 后缀是必须的。 Note that the .html extension is required.RSS系统模板渲染每一个条目,需要给传递2个参数给模板上下文变量:obj : 当前对象 ( 返回到 items() 任意对象之一 )。site : 一个表示当前站点的 django.models.core.sites.Site 对象。 这对于 {{ site.domain }} 或者{{ site.name }} 很有用。如果你在创建模板的时候,没有指明标题或者描述信息,框架会默认使用 "{{ obj }}" ,对象的字符串表示。 (For model objects, this will be the unicode() method.你也可以通过修改 Feed 类中的两个属性 title_template 和 description_template 来改变这两个模板的名字。你有两种方法来指定  的内容。 Django 首先执行 items() 中每一项的 get_absolute_url() 方法。 如果该方法不存在,就会尝试执行 Feed 类中的 item_link() 方法,并将自身作为 item 参数传递进去。get_absolute_url() 和 item_link() 都应该以Python字符串形式返回URL。对于前面提到的 LatestEntries 例子,我们可以实现一个简单的feed模板。 latest_title.html 包括:{{ obj.title }}并且 latest_description.html 包含:{{ obj.description }}这真是 太 简单了!一个更复杂的Feed框架通过参数支持更加复杂的feeds。For example, say your blog offers an RSS feed for every distinct tag you’ve used to categorize your entries. 如果为每一个单独的区域建立一个 Feed 类就显得很不明智。取而代之的方法是,使用聚合框架来产生一个通用的源,使其可以根据feeds URL返回相应的信息。Your tag-specific feeds could use URLs like this:http://example.com/feeds/tags/python/ : Returns recent entries tagged with pythonhttp://example.com/feeds/tags/cats/ : Returns recent entries tagged with cats固定的那一部分是 "beats" (区域)。举个例子会澄清一切。 下面是每个地区特定的feeds:from django.core.exceptions import ObjectDoesNotExistfrom mysite.blog.models import Entry, Tagclass TagFeed(Feed): def get_object(self, bits): # In case of "/feeds/tags/cats/dogs/mice/", or other such # clutter, check that bits has only one member. if len(bits) != 1: raise ObjectDoesNotExist return Tag.objects.get(tag=bits[0]) def title(self, obj): return "My Blog: Entries tagged with %s" % obj.tag def link(self, obj): return obj.get_absolute_url() def description(self, obj): return "Entries tagged with %s" % obj.tag def items(self, obj): entries = Entry.objects.filter(tags__id__exact=obj.id) return entries.order_by('-pub_date')[:30]以下是RSS框架的基本算法,我们假设通过URL /rss/beats/0613/ 来访问这个类:框架获得了URL /rss/beats/0613/ 并且注意到URL中的slug部分后面含有更多的信息。 它将斜杠("/" )作为分隔符,把剩余的字符串分割开作为参数,调用 Feed 类的 get_object() 方法。在这个例子中,添加的信息是 ['0613'] 。对于 /rss/beats/0613/foo/bar/ 的一个URL请求, 这些信息就是 ['0613', 'foo', 'bar'] 。get_object() 就根据给定的 bits 值来返回区域信息。In this case, it uses the Django database API to retrieve the Tag . Note that get_object() should raisedjango.core.exc

上一页  [1] [2] [3] [4] [5] [6]  下一页


Django 输出非HTML内容