sitemap框架

类似于聚合框架,Django也有一个高级的Sitemap生成框架
一个Sitemap是一个你的网站的XML文件,它告诉搜索引擎索索引你的页面的更新频率和你的站点某些页面联系到其它页面
有多"重要",这个信息帮助搜索引擎索引你的站点,参看 http://www.sitemaps.org得到更多关于Sitemaps
Django的sitemap框架通过让你用Python代码表达这个信息来自动生成这个XML文件,为了创建一个sitemap,你只需写
一个Sitemap类并在你的URL配置里指向它

安装
遵循下面的步骤来安装sitemap app:
1,添加'django.contrib.sitemaps'到你的INSTALLED_APPS设置
2,确认'django.template.loaders.app_directories.load_template_source'在你的TEMPLATE_LOADERS设置中
它默认在里面,所以如果你改变了这个设置则你将只需更改这个
3,确认你已经安装了sites框架(参考第15章)
注意,sitemap程序不会安装任何数据库表,它需要进入INSTALLED_APPS的唯一的原因是为了让load_template_source
模板载入器可以找到默认的模板

初始化
为了在你的Django站点激活sitemap生成,把下面的内容添加到你的URL配置里:
Java代码 复制代码  收藏代码
  1. (r'^sitemap.xml$''django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps})  
(r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps})

这会告诉Django当一个客户端访问/sitemap.xml时构建一个sitemap
sitemap文件名不重要,但是位置很重要,搜索引擎将只为当前及以下的URL级别索引你的sitemap里的链接
例如,如果sitemap.xml存在于你的根目录,它将引用你的站点的任何URL,如果sitemap存在于/content/sitemap.xml,
它将只引用以/content/开始的URL
sitemap使用一个额外的必需参数{'sitemaps': sitemaps},sitemaps应该是一个映射简短的section标签(如blog或者
news)到它的Sitemap类(如BlogSitemap或者NewsSitemap)的字典,它可能也映射一个Sitemap类的实例
(如BlogSitemap(some_var))

Sitemap类
一个Sitemap类是一个表示你的sitemap一部分条目的简单的Python类,例如,一个Sitemap类可以表示你的博客的所有
条目,而另一个可以表示你的日程表的所有的事件
最简单的情况下,所有这些部分混合在一个sitemap.xml里,但是也可以使用框架生成一个sitemap索引并引用单独的
sitemap文件,每个部分一个文件(参看下面的内容)
Sitemap类必须继承django.contrib.sitemaps.Sitemap,它们可以在你的代码树的任意位置
例如,让我们假设你有一个博客系统和一个Entry模型,并且你想让你的sitemap包含所有到你的单独博客条目的链接
这里是你的sitemap类的样子:
Java代码 复制代码  收藏代码
  1. from django.contrib.sitemaps import Sitemap   
  2. from mysite.blog.models import Entry   
  3.   
  4. class BlogSitemap(Sitemap):   
  5.     changefreq = "never"  
  6.     priority = 0.5  
  7.   
  8.     def items(self):   
  9.         return Entry.objects.filter(is_draft=False)   
  10.   
  11.     def lastmod(self, obj):   
  12.         return obj.pub_date  
from django.contrib.sitemaps import Sitemap
from mysite.blog.models import Entry

class 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

在看过聚合框架之后,这将看起来非常熟悉:
1,changefreq和priority是对应(changefreq)和(priority)元素的类属性,它们可以作为方法来调用,例如lastmod
2,items()是简单的返回对象列表的方法,返回的对象将根据sitemap属性(location,lastmod,changefreq和priority)
传递给任何可调用的方法
3,lastmod应该返回一个Python datetime对象
4,例子中没有location方法,但是你可以为了指出你的对象的URL而提供它,默认location()对每个对象调用
get_absolute_url()并返回结果

Sitemap方法/属性
像Feed类一样,Sitemap成员可以是方法或者属性,参考"复杂的例子"得到更多关于它怎样工作的信息
一个Sitemap类可以定义以下方法/属性:
items(必需)
提供对象列表,框架不关心它们是什么类型的对象,关心的只是这些对象传递给location(),lastmod(),changefreq()
和priority()方法
location(可选)
对给定对象提供绝对的URL
这里"绝对的URL"表示不包含协议和域名的URL,例如:
Good:'/foo/bar/'
Bad:'example.com/foo/bar/'
Bad:'http://example.com/foo/bar/'
如果location没有提供,框架将对items()返回的每个对象调用get_absolute_url()方法
lastmod(可选)
对象的"last modification"日期,是一个Python datetime对象
changefreq(可选)
对象改变的频率,可能的值(Sitemaps规范所给)为:
'always'
'hourly'
'daily'
'weekly'
'monthly'
'yearly'
'never'
priority(可选)
建议的索引优先级别,在0.0和1.0之间,一个页面的默认级别为0.5,参看sitemaps.org文档来得到更多关于priority

捷径
sitemap框架为通常的情况提供了一些方便类:
FlatPageSitemap
django.contrib.sitemaps.FlatPageSitemap类查看当前站点定义的所有flat页面并在sitemap里创建条目,这些条目只
包含location属性,不包含lastmod,changefreq或priority,参考第15章来得到更多关于flat页面
GenericSitemap
GenericSitemap类和你已经有的任何generic views(参考第9章)工作
为了使用它,创建一个实例并传递你传递给generic views的同样的info_dict,仅有的需求是这个字典有一个queryset
条目,它可能也有一个date_field条目来指定从queryset得到的对象的date域,它被用于生成的sitemap的lastmod属性
你也可以传递priority和changefreq关键字参数到GenericSitemap构造函数来为所有的URL指定这些属性
这里是一个使用FlatPageSitemap和GenericSitemap的URL配置的例子(使用上面假定的Entry对象):
Java代码 复制代码  收藏代码
  1. from django.conf.urls.defaults import *   
  2. from django.contrib.sitemaps import FlatPageSitemap, GenericSitemap   
  3. from mysite.blog.models import Entry   
  4.   
  5. info_dict = {   
  6.     'queryset': Entry.objects.all(),   
  7.     'date_field''pub_date',   
  8. }   
  9.   
  10. sitemaps = {   
  11.     'flatpages': FlatPageSitemap,   
  12.     'blog': GenericSitemap(info_dict, priority=0.6),   
  13. }   
  14.   
  15. urlpatterns = patterns('',   
  16.     # some generic view using info_dict   
  17.     # ...   
  18.   
  19.     # the sitemap   
  20.     (r'^sitemap.xml$''django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps})   
  21. )  
from django.conf.urls.defaults import *
from django.contrib.sitemaps import FlatPageSitemap, GenericSitemap
from mysite.blog.models import Entry

info_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$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps})
)


创建sitemap索引
sitemap框架也可以创建一个sitemap索引来引用单独的sitemap文件,即在你的sitemaps字典里定义的每个部分
使用上唯一的区别是:
1,你在URL配置里使用两个视图:django.contrib.sitemaps.views.index和django.contrib.sitemaps.views.sitemap
2,django.contrib.sitemaps.views.sitemap应该使用一个section关键字参数
这里是上面例子的相关的URL配置:
Java代码 复制代码  收藏代码
  1. (r'^sitemap.xml$''django.contrib.sitemaps.views.index', {'sitemaps': sitemaps})   
  2. (r'^sitemap-(?P<section>.+).xml$''django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps})  
(r'^sitemap.xml$', 'django.contrib.sitemaps.views.index', {'sitemaps': sitemaps})
(r'^sitemap-(?P<section>.+).xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps})

这将自动生成一个sitemap.xml文件,它引用sitemap-flatpages.xml和sitemap-blog.xml
而Sitemap类和sitemaps目录根本没有更改

Pinging Google
你可能当你的sitemap更改时想"ping" Google来让它知道重新索引你的站点,框架提供一个方法来做这件事:
django.contrib.sitemaps.ping_google()
注意,本书在写作时,只有Google响应sitemap pings,尽管如此,很可能Yahoo或Microsoft将很快也支持这些pings
那个时候,我们可能更改ping_google()的名字为类似于ping_search_engines(),所以确认查看最近的sitemap文档
http://www.djangoproject.com/documentation/sitemaps/
ping_google()使用一个可选的参数sitemap_url,它应该为你的站点的sitemap(即'/sitemap.xml')的绝对URL
如果这个参数没有提供,ping_google()将尝试通过倒转顺序查看你的URL配置来找出你的sitemap
如果ping_google()不能决定你的sitemap URL,它将触发django.contrib.sitemaps.SitemapNotFound异常
一个有用的调用ping_google()的方式是从一个模型的save()方法:
Java代码 复制代码  收藏代码
  1. from django.contrib.sitemaps import ping_google   
  2.   
  3. class Entry(models.Model):   
  4.     # ...   
  5.     def save(self):   
  6.         super(Entry, self).save()   
  7.         try:   
  8.             ping_google()   
  9.         except Exception:   
  10.             # Bare 'except' because we could get a variety   
  11.             # of HTTP-related exceptions.   
  12.             pass  
from django.contrib.sitemaps import ping_google

class Entry(models.Model):
    # ...
    def save(self):
        super(Entry, self).save()
        try:
            ping_google()
        except Exception:
            # Bare 'except' because we could get a variety
            # of HTTP-related exceptions.
            pass

尽管如此,一个更高效的解决方案是从一个cron脚本调用ping_google(),或者一些其它的日程任务,这个方法给Google
的服务器发送一个HTTP请求,所以你可能不想每次你调用save()都招致网络过度

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值