網站地圖是根據網站的結構、框架、內容,生成的導航網頁,是一個網站全部連接的容器。不少網站的鏈接層次比較深,蜘蛛很難抓取到,網站地圖能夠方便搜索引擎或者網絡蜘蛛抓取網站頁面,瞭解網站的架構,爲網絡蜘蛛指路,增長網站內容頁面的收錄機率。網站地圖通常存放在域名根目錄下並命名爲sitemap,好比http://www.liujiangblog.com/sitemap.xml
。數據庫
一個典型的sitemap,其內容片斷以下:django
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>http://www.liujiangblog.com/blog/9/</loc> <lastmod>2017-12-08</lastmod> <priority>0.4</priority> </url> <url> <loc>http://www.liujiangblog.com/blog/8/</loc> <lastmod>2017-12-05</lastmod> <priority>0.4</priority> </url> <url> <loc>http://www.liujiangblog.com/blog/7/</loc> <lastmod>2017-11-19</lastmod> <priority>0.4</priority> </url> # 更多內容未列出
Django自帶了一個高級的生成網站地圖的框架,咱們能夠很容易地建立出XML格式的網站地圖。建立網站地圖,只需編寫一個Sitemap類,並在URLconf中編寫對應的訪問路由。後端
1、安裝
安裝sitemap框架的步驟以下:網絡
- 在INSTALLED_APPS設置中添加'django.contrib.sitemaps' .
- 確認settings.py中的
TEMPLATES
設置包含DjangoTemplates
後端,並將APP_DIRS
選項設置爲True。其實,默認配置就是這樣的,只有當你曾經修改過這些設置,才須要調整過來。 - 確認你已經安裝sites框架. (注意: 網站地圖APP並不須要在數據庫中創建任何數據庫表。修改
INSTALLED_APPS
的惟一緣由是,以便Loader()
模板加載器能夠找到默認模板。)
2、初始化
爲了在網站上激活站點地圖生成功能,請把如下代碼添加到URLconf中:架構
from django.contrib.sitemaps.views import sitemap url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap')
當用戶訪問/sitemap.xml
時,Django將生成並返回一個網站地圖。app
網站地圖的文件名並不重要,重要的是文件的位置。搜索引擎只會索引網站的當前URL層級及下屬層級。例如,若是sitemap.xml
位於根目錄中,它會引用網站中的任何URL。 可是若是站點地圖位於/content/sitemap.xml
,則它只能引用以/content/
開頭的網址。框架
sitemap視圖須要一個額外的必需參數: {'sitemaps': sitemaps}
。sitemaps
應是一個字典,將部門的標籤(例如news或blog)映射到其 Sitemap類(例如,NewsSitemap或BlogSitemap)。也能夠映射到Sitemap類的實例(例如,BlogSitemap(some_var))。網站
3、範例
假設你有一個博客系統,擁有Entry模型,而且你但願站點地圖包含指向每篇博客文章的全部連接。 如下是Sitemap類的寫法:搜索引擎
from django.contrib.sitemaps import Sitemap from blog.models import Entry class BlogSitemap(Sitemap): changefreq = "never" priority = 0.5 def items(self): return Entry.objects.filter(is_draft=False) def lastmod(self, obj): return obj.pub_date
注意:url
- changefreq和priority分別對應於HTML頁面中的
<changefreq>
和<priority>
標籤。 - items()只是一個返回對象列表的方法。
- lastmod方法應該返回一個datetime時間對象。
- 在此示例中沒有編寫location方法,但你能夠本身增長此方法來指定對象的URL。默認狀況下,location()在每一個對象上調用
get_absolute_url()
並將返回結果做爲對象的url。也就是說,使用站點地圖的模型,好比Entry,須要在模型內部實現get_absolute_url()
方法。
4、Sitemap類詳解
class Sitemap[source]
Sitemap類能夠定義如下方法/屬性:
1. items[source]
必須定義。返回對象列表的方法。
框架不關心對象的類型,重要的是這些對象將被傳遞給location(),lastmod(),changefreq()和priority()方法。
2. location[source]
可選。 其值能夠是一個方法或屬性。
若是是一個方法, 它應該爲items()返回的對象的絕對路徑.
若是它是一個屬性,它的值應該是一個字符串,表示items()返回的每一個對象的絕對路徑。
上面所說的「絕對路徑」表示不包含協議和域名的URL。 例子:
正確:'/foo/bar/' 錯誤:'example.com/foo/bar/' 錯誤:'https://example.com/foo/bar/'
若是未提供location,框架將調用items()返回的每一個對象上的get_absolute_url()
方法。
該屬性最終反映到HTML頁面上的<loc></loc>
標籤。
3. lastmod
可選。 一個方法或屬性。表示當前條目最後的修改時間。
4. changefreq
可選。 一個方法或屬性。表示當前條目修改的頻率。
changefreq的容許值爲:
'always' 'hourly' 'daily' 'weekly' 'monthly' 'yearly' 'never'
5. priority
可選。表示當前條目在網站中的權重係數,優先級。
示例值:0.4,1.0。 頁面的默認優先級爲0.5,最高爲1.0。
6. protocol
可選的。定義網站地圖中的網址的協議('http'或'https')。
7. limit
可選的。定義網站地圖的每一個網頁上包含的最大超級連接數。
8. i18n
可選的。一個boolean屬性,定義是否應使用全部語言生成此網站地圖。默認值爲False。
5、快捷方式
sitemap框架提供了一個快捷類,幫助咱們迅速生成網站地圖:
class GenericSitemap[source]
經過它,咱們無需爲sitemap編寫單獨的視圖模塊,直接在URLCONF中,獲取對象,獲取參數,傳遞參數,設置url,以下所示,一條龍服務:
from django.conf.urls import url from django.contrib.sitemaps import GenericSitemap from django.contrib.sitemaps.views import sitemap from blog.models import Entry info_dict = { 'queryset': Entry.objects.all(), 'date_field': 'pub_date', } urlpatterns = [ # some generic view using info_dict # ... # the sitemap url(r'^sitemap\.xml$', sitemap, {'sitemaps': {'blog': GenericSitemap(info_dict, priority=0.6)}}, name='django.contrib.sitemaps.views.sitemap'), ]
6、靜態視圖的Sitemap
有時候,咱們不但願在站點地圖中出現一些靜態頁面,好比商品的詳細信息頁面。要怎麼作呢?解決方案是在items中顯式列出這些頁面的網址名稱,並在網站地圖的location方法中調用reverse()。 像下面這樣:
# sitemaps.py from django.contrib import sitemaps from django.urls import reverse class StaticViewSitemap(sitemaps.Sitemap): priority = 0.5 changefreq = 'daily' def items(self): return ['main', 'about', 'license'] def location(self, item): return reverse(item) # urls.py from django.conf.urls import url from django.contrib.sitemaps.views import sitemap from .sitemaps import StaticViewSitemap from . import views sitemaps = { 'static': StaticViewSitemap, } urlpatterns = [ url(r'^$', views.main, name='main'), url(r'^about/$', views.about, name='about'), url(r'^license/$', views.license, name='license'), # ... url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap') ]
上面作法的本質,是我先找出不想展現的頁面,而後反向選擇一下,獲取想生成站點條目的對象,最後展現到站點地圖中。你能夠簡單的理解爲‘反選’。