Django之sitemap
##########settings.py
SITE_ID=1 # Application definition # taggit tag INSTALLED_APPS = [ 'myblog', 'taggit', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.sites', 'django.contrib.sitemaps' ]
python manage.py migrate
############sitemaps.py #coding:utf-8 __author__ = 'similarface' from django.contrib.sitemaps import Sitemap from .models import Post class PostSitemap(Sitemap): changefreq='weekly' priority=0.9 def items(self): return Post.published.all() def lastmod(self,obj): return obj.publish
#########urls.py from django.conf.urls import url from django.contrib import admin from django.conf.urls import include from django.contrib.sitemaps .views import sitemap from myblog.sitemaps import PostSitemap sitemaps={ 'posts':PostSitemap, } urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^myblog/', include('myblog.urls',namespace='myblog',app_name='myblog')), url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps},name='django.contrib.sitemaps.views.sitemap'), ]
http://127.0.0.1:8000/sitemap.xml
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc> http://example.com/myblog/2016/04/20/python%E4%BB%A3%E7%A0%81/ </loc> <lastmod>2016-04-20</lastmod> <changefreq>weekly</changefreq> <priority>0.9</priority> </url> <url> <loc>http://example.com/myblog/2016/04/19/ssss/</loc> <lastmod>2016-04-19</lastmod> <changefreq>weekly</changefreq> <priority>0.9</priority> </url> <url> <loc> http://example.com/myblog/2016/04/19/python%20%E6%93%8D%E4%BD%9CFTP/ </loc> <lastmod>2016-04-19</lastmod> <changefreq>weekly</changefreq> <priority>0.9</priority> </url> ...