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

Django 输出非HTML内容

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

当前URL级别及其以下级别的链接。 用一个实例来说,如果 sitemap.xml 位于你的根目录,那么它将引用任何的URL。 然而,如果你的sitemap位于 /content/sitemap.xml ,那么它只引用以 /content/ 打头的URL。sitemap视图需要一个额外的必须的参数: {'sitemaps': sitemaps} . sitemaps should be a dictionary that maps a short section label (e.g., blog or news ) to its Sitemap class (e.g., BlogSitemap or NewsSitemap ). It may also map to an instance of a Sitemap class (e.g., BlogSitemap(some_var) ).Sitemap 类Sitemap 类展示了一个进入地图站点简单的Python类片断.例如,一个 Sitemap 类能展现所有日志入口,而另外一个能够调度所有的日历事件。 For example, one Sitemap class could represent all the entries of your weblog, while another could represent all of the events in your events calendar.在最简单的例子中,所有部分可以全部包含在一个 sitemap.xml 中,也可以使用框架来产生一个站点地图,为每一个独立的部分产生一个单独的站点文件。Sitemap 类必须是 django.contrib.sitemaps.Sitemap 的子类. 他们可以存在于您的代码树的任何地方。例如假设你有一个blog系统,有一个 Entry 的model,并且你希望你的站点地图包含所有连到你的blog入口的超链接。 你的 Sitemap 类很可能是这样的:from django.contrib.sitemaps import Sitemapfrom mysite.blog.models import Entryclass BlogSitemap(Sitemap): changefreq = "never" priority = 0.5 def items(self): return Entry.objects.filter(is_draft=False) def lastmod(self, obj): return obj.pub_date声明一个 Sitemap 和声明一个 Feed 看起来很类似;这都是预先设计好的。如同 Feed 类一样, Sitemap 成员也既可以是方法,也可以是属性。 想要知道更详细的内容,请参见上文 《一个复杂的例子》章节。一个 Sitemap 类可以定义如下 方法/属性:items (必需 ):提供对象列表。 框架并不关心对象的 类型 ;唯一关心的是这些对象会传递给 location(), lastmod() , changefreq() ,和 priority() 方法。location (可选): 给定对象的绝对URL。 绝对URL不包含协议名称和域名。 下面是一些例子:好的: '/foo/bar/' '/foo/bar/'差的: 'example.com/foo/bar/' 'example.com/foo/bar/'Bad: 'http://example.com/foo/bar/'如果没有提供 location , 框架将会在每个 items() 返回的对象上调用 get_absolute_url() 方法.lastmod (可选): 对象的最后修改日期, 作为一个Python datetime 对象. The object’s last modification date, as a Python datetime object.changefreq (可选): 对象变更的频率。 可选的值如下(详见Sitemaps文档):'always''hourly''daily''weekly''monthly''yearly''never'priority (可选): 取值范围在 0.0 and 1.0 之间,用来表明优先级。快捷方式sitemap框架提供了一些常用的类。 在下一部分中会看到。FlatPageSitemapdjango.contrib.sitemaps.FlatPageSitemap 类涉及到站点中所有的flat page,并在sitemap中建立一个入口。 但仅仅只包含 location 属性,不支持 lastmod , changefreq ,或者 priority 。参见第16章获取有关flat page的更多的内容.GenericSitemapGenericSitemap 与所有的通用视图一同工作(详见第9章)。你可以如下使用它,创建一个实例,并通过 info_dict 传递给通用视图。 唯一的要求是字典包含 queryset 这一项。 也可以用 date_field 来指明从 queryset 中取回的对象的日期域。 这会被用作站点地图中的 lastmod属性。下面是一个使用 FlatPageSitemap and GenericSiteMap (包括前面所假定的 Entry 对象)的URLconf:from django.conf.urls.defaults import *from django.contrib.sitemaps import FlatPageSitemap, GenericSitemapfrom mysite.blog.models import Entryinfo_dict = {'queryset': Entry.objects.all(),'date_field': 'pub_date',}sitemaps = {'flatpages': FlatPageSitemap,'blog': GenericSitemap(info_dict, priority=0.6),}urlpatterns = patterns('',some generic view using info_dict# ...# the sitemap(r'^sitemap\.xml/pre>, 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}))创建一个Sitemap索引sitemap框架同样可以根据 sitemaps 字典中定义的单独的sitemap文件来建立索引。 用法区别如下:您在您的URLconf 中使用了两个视图: django.contrib.sitemaps.views.index 和django.contrib.sitemaps.views.sitemap . django.contrib.sitemaps.views.index 和django.contrib.sitemaps.views.sitemapdjango.contrib.sitemaps.views.sitemap 视图需要带一个 section 关键字参数.这里是前面的例子的相关的 URLconf 行看起来的样子:(r'^sitemap.xml/pre>,'django.contrib.sitemaps.views.index',{'sitemaps': sitemaps}),(r'^sitemap-(?P.+).xml/pre>,'django.contrib.sitemaps.views.sitemap',{'sitemaps': sitemaps})这将自动生成一个 sitemap.xml 文件, 它同时引用 sitemap-flatpages.xml 和 sitemap-blog.xml . Sitemap 类和sitemaps 目录根本没有更改.通知Google当你的sitemap变化的时候,你会想通知Google,以便让它知道对你的站点进行重新索引。 框架就提供了这样的一个函数: django.contrib.sitemaps.ping_google() 。ping_google() 有一个可选的参数 sitemap_url ,它应该是你的站点地图的URL绝对地址(例如:如果不能够确定你的sitemap URL, ping_google() 会引发 django.contrib.sitemaps.SitemapNotFound 异常。我们可以通过模型中的 save() 方法来调用 ping_google() :from django.contrib.sitemaps import ping_googleclass Entry(models.Model): # ... def save(self, *args, **kwargs): super(Entry, self).save(*args, **kwargs) try: ping_google() except Exception: # Bare 'except' because we could get a variety # of HTTP-related exceptions. pass一个更有效的解决方案是用 cron 脚本或任务调度表来调用 ping_google() ,该方法使用Http直接请求Google服务器,从而减少每次调用 save() 时占用的网络带宽。 The function makes an HTTP request to Google’s servers, so you may not want to introduce that

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


Django 输出非HTML内容