在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)參見 http://www.sitemaps.org/.
Django sitemap 框架允許你用 Python 代碼來(lái)表述這些信息,從而自動(dòng)創(chuàng)建這個(gè)XML文件。 要?jiǎng)?chuàng)建一個(gè)站點(diǎn)地圖,你只需要寫一個(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)用程序沒有安裝任何數(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è)你有一個(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/'
如果沒有提供 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ì)象變更的頻率。 可選的值如下(詳見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 與所有的通用視圖一同工作(詳見第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 目錄根本沒有更改.
通知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)文章
Matplotlib實(shí)戰(zhàn)之折線圖繪制詳解
折線圖是一種用于可視化數(shù)據(jù)變化趨勢(shì)的圖表,它可以用于表示任何數(shù)值隨著時(shí)間或類別的變化,本文主要介紹了如何利用Matplotlib實(shí)現(xiàn)折線圖的繪制,感興趣的可以了解下2023-08-08
Python利用自帶模塊實(shí)現(xiàn)屏幕像素高效操作
這篇文章主要為大家詳細(xì)介紹了Python如何利用自帶模塊實(shí)現(xiàn)屏幕像素高效操作,文中的示例代碼講解詳,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-02-02
python題解LeetCode303區(qū)域和檢索示例詳解
這篇文章主要為大家介紹了python題解LeetCode303區(qū)域和檢索示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
對(duì)tensorflow 中tile函數(shù)的使用詳解
今天小編就為大家分享一篇對(duì)tensorflow 中tile函數(shù)的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02
Python OpenCV實(shí)現(xiàn)鼠標(biāo)畫框效果
這篇文章主要為大家詳細(xì)介紹了Python OpenCV實(shí)現(xiàn)鼠標(biāo)畫框效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08
關(guān)于安裝halcon包pycharm提示不全的問(wèn)題
很多小伙伴給小編反映在pycham上面安裝halcon對(duì)應(yīng)的安裝包之后,導(dǎo)入出現(xiàn)問(wèn)題,發(fā)現(xiàn)輸入ha.read 沒有自動(dòng)提示 ,只有幾個(gè)變量和方法,怎么解決這個(gè)問(wèn)題呢,下面小編給大家?guī)?lái)了安裝halcon包pycharm提示不全的問(wèn)題,一起看看吧2021-06-06
python twilio模塊實(shí)現(xiàn)發(fā)送手機(jī)短信功能
這篇文章主要介紹了python twilio模塊實(shí)現(xiàn)發(fā)送手機(jī)短信的功能,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08

