码迷,mamicode.com
首页 > 其他好文 > 详细

Django之sitemap

时间:2016-04-20 13:07:36      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

##########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>
...

 

Django之sitemap

标签:

原文地址:http://www.cnblogs.com/similarface/p/5411969.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!