官方文档地址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文件

本文介绍了如何在Django项目中添加网站地图支持。步骤包括在install_app中引入'django.contrib.sitemaps',确保'TEMPLATE_LOADERS'设置包含'app_directories.Loader',配置URL路由,以及创建网站地图类。注意,当网站地图内容过多时,可考虑使用多个sitemap文件来管理。
2257

被折叠的 条评论
为什么被折叠?



