国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

在Django中使用Sitemap的方法講解

2019-11-25 17:08:49
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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`` 類(lèi),并且在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 類(lèi)

Sitemap 類(lèi)展示了一個(gè)進(jìn)入地圖站點(diǎn)簡(jiǎn)單的Python類(lèi)片斷.例如,一個(gè) Sitemap 類(lèi)能展現(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 類(lèi)必須是 django.contrib.sitemaps.Sitemap 的子類(lèi). 他們可以存在于您的代碼樹(shù)的任何地方。

例如假設(shè)你有一個(gè)blog系統(tǒng),有一個(gè) Entry 的model,并且你希望你的站點(diǎn)地圖包含所有連到你的blog入口的超鏈接。 你的 Sitemap 類(lèi)很可能是這樣的:

from django.contrib.sitemaps import Sitemapfrom mysite.blog.models import Entryclass 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)很類(lèi)似;這都是預(yù)先設(shè)計(jì)好的。

如同 Feed 類(lèi)一樣, Sitemap 成員也既可以是方法,也可以是屬性。

一個(gè) Sitemap 類(lèi)可以定義如下 方法/屬性:

    items (必需 ):提供對(duì)象列表。 框架并不關(guān)心對(duì)象的 類(lèi)型 ;唯一關(guān)心的是這些對(duì)象會(huì)傳遞給 location() , lastmod() , changefreq() ,和 priority() 方法。

    location (可選): 給定對(duì)象的絕對(duì)URL。 絕對(duì)URL不包含協(xié)議名稱(chēng)和域名。 下面是一些例子:

  •         好的: '/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框架提供了一些常用的類(lèi)。 在下一部分中會(huì)看到。
FlatPageSitemap

django.contrib.sitemaps.FlatPageSitemap 類(lèi)涉及到站點(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, GenericSitemapfrom mysite.blog.models import Entryinfo_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 類(lèi)和 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_googleclass 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


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 陇南市| 逊克县| 什邡市| 台中县| 桃江县| 寿宁县| 建德市| 太仆寺旗| 白山市| 体育| 蚌埠市| 庄浪县| 绍兴市| 阜康市| 密山市| 宝应县| 金乡县| 尼木县| 新乐市| 苍南县| 安徽省| 永新县| 本溪市| 侯马市| 克拉玛依市| 深泽县| 定西市| 金门县| 黎城县| 武宣县| 翁牛特旗| 青阳县| 山丹县| 巢湖市| 登封市| 峨眉山市| 鄂州市| 武城县| 安远县| 涟水县| 保德县|