我有一个博客网站,我想生成2个站点地图,一个用于帖子,一个用于我的代码类别:
sitemaps.py
from django.contrib.sitemaps import Sitemap
from .models import Post, Category
class PostSitemap(Sitemap):
priority = 0.5
def items(self):
posts = Post.objects.filter(is_published=True).order_by('-updated_at')
return posts
def lastmod(self, obj):
return obj.updated_at
class CategorySitemap(Sitemap):
priority = 0.5
def items(self):
categories = Category.objects.filter(
is_published=True).order_by('-updated_at')
return categories
def lastmod(self, obj):
return obj.updated_at
urls.py
from .sitemap import PostSitemap, CategorySitemap
from django.contrib.sitemaps.views import sitemap
sitemaps = {
'posts': PostSitemap,
'categories': CategorySitemap,
}
urlpatterns = [
url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps}),
]
以上代码是使用django创建动态站点地图的正确方法;