官方文档地址https://docs.djangoproject.com/en/dev/ref/contrib/sitemaps/
使用起来相当简单
1:install_app内增加 'django.contrib.sitemaps'
2:Make sure 'django.template.loaders.app_directories.Loader' is in your TEMPLATE_LOADERS setting
确认设置TEMPLATE_LOADERS变量中包含'django.template.loaders.app_directories.Loader'选项
3:url中 添加
(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps})
4:添加地图类
from django.contrib.sitemaps import Sitemap from models import * from django.db.models import Sum class ZnanrenSitemaps(Sitemap): changefreq = 'daily' # priority = 0.5 def items(self): return News.objects.all() def lastmod(self , obj): if obj.update_date: return obj.update_date return obj.created_date def priority(self , obj): if obj.readCount: allReadCount = News.objects.all().aggregate(Sum('readCount'))['readCount__sum'] curPriority = obj.readCount / float(allReadCount) if obj.figthingCount: fightCount = News.objects.all().aggregate(Sum('figthingCount'))['figthingCount__sum'] return '%.2f' % (((obj.figthingCount / float(fightCount) + curPriority) / 2.0) / 2.0 + 0.5) return '%.2f' % (curPriority / 2.0 + 0.5) return 0.50
从sitemap类中可以看到支持参数
for item in self.paginator.page(page).object_list: loc = "%s://%s%s" % (protocol, domain, self.__get('location', item)) priority = self.__get('priority', item, None) url_info = { 'item': item, 'location': loc, 'lastmod': self.__get('lastmod', item, None), 'changefreq': self.__get('changefreq', item, None), 'priority': str(priority is not None and priority or ''), } urls.append(url_info) return urls
备注:
地图xml参数部分 查看这里http://www.admin5.com/article/20080825/100474.shtml
网站地图数量太大。查阅文档。采用多个sitemap文件