# settings.py 添加 apps: 'django.contrib.sites', 'django.contrib.sitemaps',
代码语言:javascript复制INSTALLED_APPS = [
'django.contrib.sites',
'django.contrib.sitemaps',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates').replace('\', '/') # 设置模版路径
]
,
'APP_DIRS': True, # 设置为 True
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
# sitmaps.py
代码语言:javascript复制#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: yinzhuoqun
@site: http://zhuoqun.info/
@email: yin@zhuoqun.info
@time: 2018/8/7 17:34
"""
# from django.contrib.sitemaps import Sitemap
from django.contrib import sitemaps
from blog.models import Article, Tag, Node, Classifing, CommentTemporary, Quotations
from django.contrib.sitemaps import GenericSitemap
from django.urls import reverse
class StaticViewSitemap(sitemaps.Sitemap):
priority = 0.5 # 权重
changefreq = 'daily' # 更新频率
def items(self):
return ['blog:index', ]
def location(self, item):
return reverse(item)
class ArticleSiteMap(sitemaps.Sitemap):
"""
items (必需 ):提供对象列表。 框架并不关心对象的 类型 ;唯一关心的是这些对象会传递给 location() , lastmod() , changefreq() ,和 priority() 方法。
"""
changefreq = "monthly"
priority = "0.6"
def items(self):
return Article.objects.filter(show_status=True)
def lastmod(self, obj):
return obj.time_updated
# urls.py
代码语言:javascript复制from .sitemaps import StaticViewSitemap, ArticleSiteMap
# sitemaps
sitemaps = {
'static': StaticViewSitemap,
'blog': ArticleSiteMap,
}
urlpatterns = [
url(r'^sitemap.xml$', sitemap, {'sitemaps': sitemaps},
name='django.contrib.sitemaps.views.sitemap'),
]
# settings.py, SITE_ID 设置 xml 中 loc 显示的值,django sitemap.xml 修改 loc 显示的 example.com
代码语言:javascript复制# django_site 表的 ID 值, 1 是 zhuoqun.info, 2 是 xieboke.net
SITE_ID = 2