在Django中使用Sitemap的方法講解
sitemap 是你服務(wù)器上的一個(gè)XML文件,它告訴搜索引擎你的頁(yè)面的更新頻率和某些頁(yè)面相對(duì)于其它頁(yè)面的重要性。 這個(gè)信息會(huì)幫助搜索引擎索引你的網(wǎng)站。
例如,這是 Django 網(wǎng)站(http://www.djangoproject.com/sitemap.xml)sitemap的一部分:
<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>http://www.djangoproject.com/documentation/</loc> <changefreq>weekly</changefreq> <priority>0.5</priority> </url> <url> <loc>http://www.djangoproject.com/documentation/0_90/</loc> <changefreq>never</changefreq> <priority>0.1</priority> </url> ... </urlset>
需要了解更多有關(guān) sitemaps 的信息, 請(qǐng)參見(jiàn) http://www.sitemaps.org/.
Django sitemap 框架允許你用 Python 代碼來(lái)表述這些信息,從而自動(dòng)創(chuàng)建這個(gè)XML文件。 要?jiǎng)?chuàng)建一個(gè)站點(diǎn)地圖,你只需要寫(xiě)一個(gè)`` Sitemap`` 類,并且在URLconf中指向它。
安裝
要安裝 sitemap 應(yīng)用程序, 按下面的步驟進(jìn)行:
- 將 'django.contrib.sitemaps' 添加到您的 INSTALLED_APPS 設(shè)置中.
- 確保 'django.template.loaders.app_directories.load_template_source' 在您的 TEMPLATE_LOADERS 設(shè)置中。 默認(rèn)情況下它在那里, 所以, 如果你已經(jīng)改變了那個(gè)設(shè)置的話, 只需要改回來(lái)即可。
- 確定您已經(jīng)安裝了 sites 框架.
Note
sitemap 應(yīng)用程序沒(méi)有安裝任何數(shù)據(jù)庫(kù)表. 它需要加入到 INSTALLED_APPS 中的唯一原因是: 這樣 load_template_source 模板加載器可以找到默認(rèn)的模板. The only reason it needs to go into INSTALLED_APPS is so the load_template_source template loader can find the default templates.
Initialization
要在您的Django站點(diǎn)中激活sitemap生成, 請(qǐng)?jiān)谀?URLconf 中添加這一行:
(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps})
This line tells Django to build a sitemap when a client accesses /sitemap.xml . Note that the dot character in sitemap.xml is escaped with a backslash, because dots have a special meaning in regular expressions.
sitemap文件的名字無(wú)關(guān)緊要,但是它在服務(wù)器上的位置卻很重要。 搜索引擎只索引你的sitemap中當(dāng)前URL級(jí)別及其以下級(jí)別的鏈接。 用一個(gè)實(shí)例來(lái)說(shuō),如果 sitemap.xml 位于你的根目錄,那么它將引用任何的URL。 然而,如果你的sitemap位于 /content/sitemap.xml ,那么它只引用以 /content/ 打頭的URL。
sitemap視圖需要一個(gè)額外的必須的參數(shù): {'sitemaps': sitemaps} . sitemaps should be a dictionary that maps a short section label (e.g., blog or news ) to its Sitemap class (e.g., BlogSitemap or NewsSitemap ). It may also map to an instance of a Sitemap class (e.g., BlogSitemap(some_var) ).
Sitemap 類
Sitemap 類展示了一個(gè)進(jìn)入地圖站點(diǎn)簡(jiǎn)單的Python類片斷.例如,一個(gè) Sitemap 類能展現(xiàn)所有日志入口,而另外一個(gè)能夠調(diào)度所有的日歷事件。 For example, one Sitemap class could represent all the entries of your weblog, while another could represent all of the events in your events calendar.
在最簡(jiǎn)單的例子中,所有部分可以全部包含在一個(gè) sitemap.xml 中,也可以使用框架來(lái)產(chǎn)生一個(gè)站點(diǎn)地圖,為每一個(gè)獨(dú)立的部分產(chǎn)生一個(gè)單獨(dú)的站點(diǎn)文件。
Sitemap 類必須是 django.contrib.sitemaps.Sitemap 的子類. 他們可以存在于您的代碼樹(shù)的任何地方。
例如假設(shè)你有一個(gè)blog系統(tǒng),有一個(gè) Entry 的model,并且你希望你的站點(diǎn)地圖包含所有連到你的blog入口的超鏈接。 你的 Sitemap 類很可能是這樣的:
from django.contrib.sitemaps import Sitemap from mysite.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
聲明一個(gè) Sitemap 和聲明一個(gè) Feed 看起來(lái)很類似;這都是預(yù)先設(shè)計(jì)好的。
如同 Feed 類一樣, Sitemap 成員也既可以是方法,也可以是屬性。
一個(gè) Sitemap 類可以定義如下 方法/屬性:
items (必需 ):提供對(duì)象列表。 框架并不關(guān)心對(duì)象的 類型 ;唯一關(guān)心的是這些對(duì)象會(huì)傳遞給 location() , lastmod() , changefreq() ,和 priority() 方法。
location (可選): 給定對(duì)象的絕對(duì)URL。 絕對(duì)URL不包含協(xié)議名稱和域名。 下面是一些例子:
- 好的: '/foo/bar/' '/foo/bar/'
- 差的: 'example.com/foo/bar/' 'example.com/foo/bar/'
如果沒(méi)有提供 location , 框架將會(huì)在每個(gè) items() 返回的對(duì)象上調(diào)用 get_absolute_url() 方法.
lastmod (可選): 對(duì)象的最后修改日期, 作為一個(gè)Python datetime 對(duì)象. The object's last modification date, as a Python datetime object.
changefreq (可選): 對(duì)象變更的頻率。 可選的值如下(詳見(jiàn)Sitemaps文檔):
- 'always'
- 'hourly'
- 'daily'
- 'weekly'
- 'monthly'
- 'yearly'
- 'never'
- priority (可選): 取值范圍在 0.0 and 1.0 之間,用來(lái)表明優(yōu)先級(jí)。
快捷方式
sitemap框架提供了一些常用的類。 在下一部分中會(huì)看到。
FlatPageSitemap
django.contrib.sitemaps.FlatPageSitemap 類涉及到站點(diǎn)中所有的flat page,并在sitemap中建立一個(gè)入口。 但僅僅只包含 location 屬性,不支持 lastmod , changefreq ,或者 priority 。
GenericSitemap
GenericSitemap 與所有的通用視圖一同工作(詳見(jiàn)第9章)。
你可以如下使用它,創(chuàng)建一個(gè)實(shí)例,并通過(guò) info_dict 傳遞給通用視圖。 唯一的要求是字典包含 queryset 這一項(xiàng)。 也可以用 date_field 來(lái)指明從 queryset 中取回的對(duì)象的日期域。 這會(huì)被用作站點(diǎn)地圖中的 lastmod 屬性。
下面是一個(gè)使用 FlatPageSitemap and GenericSiteMap (包括前面所假定的 Entry 對(duì)象)的URLconf:
from django.conf.urls.defaults import * from django.contrib.sitemaps import FlatPageSitemap, GenericSitemap from mysite.blog.models import Entry info_dict = { 'queryset': Entry.objects.all(), 'date_field': 'pub_date', } sitemaps = { 'flatpages': FlatPageSitemap, 'blog': GenericSitemap(info_dict, priority=0.6), } urlpatterns = patterns('', # some generic view using info_dict # ... # the sitemap (r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}) )
創(chuàng)建一個(gè)Sitemap索引
sitemap框架同樣可以根據(jù) sitemaps 字典中定義的單獨(dú)的sitemap文件來(lái)建立索引。 用法區(qū)別如下:
您在您的URLconf 中使用了兩個(gè)視圖: django.contrib.sitemaps.views.index 和 django.contrib.sitemaps.views.sitemap . `` django.contrib.sitemaps.views.index`` 和`` django.contrib.sitemaps.views.sitemap``
django.contrib.sitemaps.views.sitemap 視圖需要帶一個(gè) section 關(guān)鍵字參數(shù).
這里是前面的例子的相關(guān)的 URLconf 行看起來(lái)的樣子:
(r'^sitemap.xml$', 'django.contrib.sitemaps.views.index', {'sitemaps': sitemaps}), (r'^sitemap-(?P<section>.+).xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps})
這將自動(dòng)生成一個(gè) sitemap.xml 文件, 它同時(shí)引用 sitemap-flatpages.xml 和 sitemap-blog.xml . Sitemap 類和 sitemaps 目錄根本沒(méi)有更改.
通知Google
當(dāng)你的sitemap變化的時(shí)候,你會(huì)想通知Google,以便讓它知道對(duì)你的站點(diǎn)進(jìn)行重新索引。 框架就提供了這樣的一個(gè)函數(shù): django.contrib.sitemaps.ping_google() 。
ping_google() 有一個(gè)可選的參數(shù) sitemap_url ,它應(yīng)該是你的站點(diǎn)地圖的URL絕對(duì)地址(例如:
如果不能夠確定你的sitemap URL, ping_google() 會(huì)引發(fā) django.contrib.sitemaps.SitemapNotFound 異常。
我們可以通過(guò)模型中的 save() 方法來(lái)調(diào)用 ping_google() :
from django.contrib.sitemaps import ping_google class Entry(models.Model): # ... def save(self, *args, **kwargs): super(Entry, self).save(*args, **kwargs) try: ping_google() except Exception: # Bare 'except' because we could get a variety # of HTTP-related exceptions. pass
一個(gè)更有效的解決方案是用 cron 腳本或任務(wù)調(diào)度表來(lái)調(diào)用 ping_google() ,該方法使用Http直接請(qǐng)求Google服務(wù)器,從而減少每次調(diào)用 save() 時(shí)占用的網(wǎng)絡(luò)帶寬。 The function makes an HTTP request to Google's servers, so you may not want to introduce that network overhead each time you call save() .
Finally, if 'django.contrib.sitemaps' is in your INSTALLED_APPS , then your manage.py will include a new command, ping_google . This is useful for command-line access to pinging. For example:
python manage.py ping_google /sitemap.xml
相關(guān)文章
python異步編程之a(chǎn)syncio低階API的使用詳解
asyncio中低階API的種類很多,涉及到開(kāi)發(fā)的5個(gè)方面,這篇文章主要為大家詳細(xì)介紹了這些低階API的具體使用,感興趣的小伙伴可以學(xué)習(xí)一下2024-01-01利用python numpy+matplotlib繪制股票k線圖的方法
這篇文章主要介紹了利用python numpy+matplotlib繪制股票k線圖的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06基于Python實(shí)現(xiàn)自動(dòng)關(guān)機(jī)小工具
上班族經(jīng)常會(huì)遇到這樣情況,著急下班結(jié)果將關(guān)機(jī)誤點(diǎn)成重啟,或者臨近下班又通知開(kāi)會(huì),開(kāi)完會(huì)已經(jīng)遲了還要去給電腦關(guān)機(jī)。今天使用PyQt5做了個(gè)自動(dòng)關(guān)機(jī)的小工具,設(shè)置好關(guān)機(jī)時(shí)間然后直接提交即可,需要的可以參考一下2022-10-10Python Social Auth構(gòu)建靈活而強(qiáng)大的社交登錄系統(tǒng)實(shí)例探究
這篇文章主要為大家介紹了Python Social Auth構(gòu)建靈活而強(qiáng)大的社交登錄系統(tǒng)實(shí)例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01Python函數(shù)的作用域及內(nèi)置函數(shù)詳解
這篇文章主要介紹了python函數(shù)的作用域及內(nèi)置函數(shù)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-09-09django使用xlwt導(dǎo)出excel文件實(shí)例代碼
這篇文章主要介紹了django使用xlwt導(dǎo)出excel文件實(shí)例代碼,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02