You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DjangoBlog/djangoblog/sitemap.py

70 lines
2.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#gq:
from django.contrib.sitemaps import Sitemap # Django内置站点地图生成类
from django.urls import reverse # URL反向解析
from blog.models import Article, Category, Tag # 导入博客相关模型
class StaticViewSitemap(Sitemap):
"""静态页面站点地图:如首页"""
priority = 0.5 # 页面优先级0.0-1.0
changefreq = 'daily' # 内容更新频率
def items(self):
"""返回要包含的静态页面名称列表对应URL名称"""
return ['blog:index', ]
def location(self, item):
"""返回每个静态页面的URL"""
return reverse(item)
class ArticleSiteMap(Sitemap):
"""文章页面站点地图"""
changefreq = "monthly" # 文章更新频率
priority = "0.6" # 文章页面优先级
def items(self):
"""返回所有已发布status='p')的文章"""
return Article.objects.filter(status='p')
def lastmod(self, obj):
"""返回文章的最后修改时间"""
return obj.last_modify_time
class CategorySiteMap(Sitemap):
"""分类页面站点地图"""
changefreq = "Weekly" # 分类更新频率
priority = "0.6" # 分类页面优先级
def items(self):
"""返回所有分类"""
return Category.objects.all()
def lastmod(self, obj):
"""返回分类的最后修改时间"""
return obj.last_modify_time
class TagSiteMap(Sitemap):
"""标签页面站点地图"""
changefreq = "Weekly" # 标签更新频率
priority = "0.3" # 标签页面优先级(低于文章和分类)
def items(self):
"""返回所有标签"""
return Tag.objects.all()
def lastmod(self, obj):
"""返回标签的最后修改时间"""
return obj.last_modify_time
class UserSiteMap(Sitemap):
"""用户页面站点地图"""
changefreq = "Weekly" # 用户信息更新频率
priority = "0.3" # 用户页面优先级
def items(self):
"""返回所有发布过文章的不重复作者列表"""
return list(set(map(lambda x: x.author, Article.objects.all())))
def lastmod(self, obj):
"""返回用户的注册时间(作为站点地图的最后更新时间)"""
return obj.date_joined