1. 程式人生 > >django網站地圖sitemap

django網站地圖sitemap


網站地圖是根據網站的結構、框架、內容,生成的導航網頁,是一個網站所有連結的容器。很多網站的連線層次比較深,蜘蛛很難抓取到,網站地圖可以方便搜尋引擎或者網路蜘蛛抓取網站頁面,瞭解網站的架構,為網路蜘蛛指路,增加網站內容頁面的收錄概率。網站地圖一般存放在域名根目錄下並命名為sitemap,比如http://www.liujiangblog.com/sitemap.xml

一個典型的sitemap,其內容片段如下:

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中編寫對應的訪問路由。

一、安裝

安裝sitemap框架的步驟如下:

  1. 在INSTALLED_APPS設定中新增'django.contrib.sitemaps' .
  2. 確認settings.py中的TEMPLATES設定包含DjangoTemplates後端,並將APP_DIRS選項設定為True。其實,預設配置就是這樣的,只有當你曾經修改過這些設定,才需要調整過來。
  3. 確認你已經安裝sites框架. (注意: 網站地圖APP並不需要在資料庫中建立任何資料庫表。修改INSTALLED_APPS
    的唯一原因是,以便Loader()模板載入器可以找到預設模板。)

二、初始化

為了在網站上啟用站點地圖生成功能,請把以下程式碼新增到URLconf中:

from django.contrib.sitemaps.views import sitemap

url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap') 

當用戶訪問/sitemap.xml時,Django將生成並返回一個網站地圖。

網站地圖的檔名並不重要,重要的是檔案的位置。搜尋引擎只會索引網站的當前URL層級及下屬層級。例如,如果sitemap.xml

位於根目錄中,它會引用網站中的任何URL。 但是如果站點地圖位於/content/sitemap.xml,則它只能引用以/content/開頭的網址。

sitemap檢視需要一個額外的必需引數: {'sitemaps': sitemaps}sitemaps應是一個字典,將部門的標籤(例如news或blog)對映到其 Sitemap類(例如,NewsSitemap或BlogSitemap)。也可以對映到Sitemap類的例項(例如,BlogSitemap(some_var))。

三、範例

假設你有一個部落格系統,擁有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 

注意:

  • changefreq和priority分別對應於HTML頁面中的<changefreq><priority>標籤。
  • items()只是一個返回物件列表的方法。
  • lastmod方法應該返回一個datetime時間物件。
  • 在此示例中沒有編寫location方法,但你可以自己增加此方法來指定物件的URL。預設情況下,location()在每個物件上呼叫get_absolute_url()並將返回結果作為物件的url。也就是說,使用站點地圖的模型,比如Entry,需要在模型內部實現get_absolute_url()方法。

四、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。

五、快捷方式

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'), ] 

六、靜態檢視的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') ] 

上面做法的本質,是我先找出不想展示的頁面,然後反向選擇一下,獲取想生成站點條目的物件,最後展示到站點地圖中。你可以簡單的理解為‘反選’。