我按照Django 2.0(暂时无法升级)官方文档添加了站点地图。 当访问我的生产站点时,我得到了所有带有日期的正确链接,但都是纯文本形式,没有 xml 格式。谷歌搜索控制台不接受这种格式。
我的sitemap.py代码:
from blog.models import Post
from django.contrib.sitemaps import Sitemap
from courses.models import Course, Interview, Lesson
class PostSitemap(Sitemap):
changefreq = 'weekly'
priority = 0.9
def items(self):
return Post.objects.all()
def lastmod(self, obj):
return obj.updated
...
我的urls.py代码:
path(
"sitemap.xml",
sitemap,
{"sitemaps": {
"courses": CourseSitemap,
"posts": PostSitemap,
"lessons": LessonSitemap,
"podcasts": InterviewSitemap,
}},
name="django.contrib.sitemaps.views.sitemap",
),