Django sitemaps架設
如何建置django提供的sitemaps呢?首先
先確定你的專案是否有加入了sites以及sitemaps
以及TEMPLATES下的APP_DIRS是否是True
settings.py
INSTALLED_APPS = (
'django.contrib.sites',
'django.contrib.sitemaps',
)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': 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',
],
},
},
]
接著我們創建一個sitemaps.py在專案底下(哪邊都可以)
sitemaps.py
from django.contrib.sitemaps import Sitemap
from datetime import datetime
from django.core.urlresolvers import reverse
class test(Sitemap):
def __init__(self, naems):
self.names = names
def items(self):
return self.names
def changefreq(self, obj):
return 'weekly'
def lastmod(self, obj):
return datetime.now()
//取得該url name 的網址
def location(self, obj):
return reverse(obj)
然後我們回到我們的urls.py
urls.py
//import 剛剛創建的sitemaps.py 底下的class from myprojest.sitemaps import test //創一個dictionary關於sitemaps的對照
sitemaps = {
//class帶進去的參數是url name
'pages':test(['login_index'], ['logout_index'])
}
urlpatterns = [
//這就是我提到的url name
url(r'^login/', memver.views.LoginView, name='login_index'),
url(r'^logout/', memver.views.LogoutView, name='logout_index'),
//關鍵在這了 sitemaps就是剛剛上面創建的dictionary
url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps':sitemaps}),
]
結束後快點去瀏覽器輸入你的domain + /sitemap.xml 就能看到結果了
result:
<urlset>
<url>
<loc>http://https://l-somewhere.com/login/</loc>
<lastmod>2015-09-03</lastmod>
<changefreq>weekly</changefreq>
</url>
</urlset>
訂閱:
張貼留言
(
Atom
)
技術提供:Blogger.
沒有留言 :
張貼留言