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.
Django/doc/djangoblog/sitemap.py

81 lines
2.4 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.

from django.contrib.sitemaps import Sitemap
from django.urls import reverse
# 导入博客相关模型
from blog.models import Article, Category, Tag
class StaticViewSitemap(Sitemap):
"""静态页面站点地图"""
priority = 0.5 # 优先级0.0-1.0
changefreq = 'daily' # 更新频率:每天
def items(self):
"""返回包含在站点地图中的项目列表"""
return ['blog:index', ] # 博客首页
def location(self, item):
"""返回每个项目的绝对URL"""
return reverse(item) # 通过反向解析生成URL
class ArticleSiteMap(Sitemap):
"""文章站点地图"""
changefreq = "monthly" # 更新频率:每月
priority = "0.6" # 优先级0.6
def items(self):
"""返回所有已发布的文章"""
return Article.objects.filter(status='p') # 状态为'p'(已发布)的文章
def lastmod(self, obj):
"""返回文章的最后修改时间"""
return obj.last_modify_time # 文章的最后修改时间
class CategorySiteMap(Sitemap):
"""分类站点地图"""
changefreq = "Weekly" # 更新频率:每周
priority = "0.6" # 优先级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" # 优先级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" # 优先级0.3(用户页优先级较低)
def items(self):
"""返回所有有文章的作者(去重)"""
# 获取所有文章的作者并通过set去重再转换为列表
return list(set(map(lambda x: x.author, Article.objects.all())))
def lastmod(self, obj):
"""返回用户的注册时间"""
return obj.date_joined # 用户的注册时间