欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

在Django中使用Sitemap的方法講解

 更新時(shí)間:2015年07月22日 15:22:26   投稿:goldensun  
這篇文章主要介紹了在Django中使用Sitemap的方法講解,Django是最具人氣的Python web開發(fā)框架,需要的朋友可以參考下

sitemap 是你服務(wù)器上的一個(gè)XML文件,它告訴搜索引擎你的頁面的更新頻率和某些頁面相對于其它頁面的重要性。 這個(gè)信息會幫助搜索引擎索引你的網(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 的信息, 請參見 http://www.sitemaps.org/.

Django sitemap 框架允許你用 Python 代碼來表述這些信息,從而自動創(chuàng)建這個(gè)XML文件。 要創(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è)置的話, 只需要改回來即可。
  •     確定您已經(jīng)安裝了 sites 框架.

Note

sitemap 應(yīng)用程序沒有安裝任何數(shù)據(jù)庫表. 它需要加入到 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生成, 請?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文件的名字無關(guān)緊要,但是它在服務(wù)器上的位置卻很重要。 搜索引擎只索引你的sitemap中當(dāng)前URL級別及其以下級別的鏈接。 用一個(gè)實(shí)例來說,如果 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)簡單的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.

在最簡單的例子中,所有部分可以全部包含在一個(gè) sitemap.xml 中,也可以使用框架來產(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 看起來很類似;這都是預(yù)先設(shè)計(jì)好的。

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

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

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

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

  •         好的: '/foo/bar/' '/foo/bar/'
  •         差的: 'example.com/foo/bar/' 'example.com/foo/bar/'

    如果沒有提供 location , 框架將會在每個(gè) items() 返回的對象上調(diào)用 get_absolute_url() 方法.

    lastmod (可選): 對象的最后修改日期, 作為一個(gè)Python datetime 對象. The object's last modification date, as a Python datetime object.

    changefreq (可選): 對象變更的頻率。 可選的值如下(詳見Sitemaps文檔):

  •         'always'
  •         'hourly'
  •         'daily'
  •         'weekly'
  •         'monthly'
  •         'yearly'
  •         'never'
  •     priority (可選): 取值范圍在 0.0 and 1.0 之間,用來表明優(yōu)先級。

快捷方式

sitemap框架提供了一些常用的類。 在下一部分中會看到。
FlatPageSitemap

django.contrib.sitemaps.FlatPageSitemap 類涉及到站點(diǎn)中所有的flat page,并在sitemap中建立一個(gè)入口。 但僅僅只包含 location 屬性,不支持 lastmod , changefreq ,或者 priority 。

GenericSitemap

GenericSitemap 與所有的通用視圖一同工作(詳見第9章)。

你可以如下使用它,創(chuàng)建一個(gè)實(shí)例,并通過 info_dict 傳遞給通用視圖。 唯一的要求是字典包含 queryset 這一項(xiàng)。 也可以用 date_field 來指明從 queryset 中取回的對象的日期域。 這會被用作站點(diǎn)地圖中的 lastmod 屬性。

下面是一個(gè)使用 FlatPageSitemap and GenericSiteMap (包括前面所假定的 Entry 對象)的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文件來建立索引。 用法區(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 行看起來的樣子:

(r'^sitemap.xml$',
 'django.contrib.sitemaps.views.index',
 {'sitemaps': sitemaps}),

(r'^sitemap-(?P<section>.+).xml$',
 'django.contrib.sitemaps.views.sitemap',
 {'sitemaps': sitemaps})

這將自動生成一個(gè) sitemap.xml 文件, 它同時(shí)引用 sitemap-flatpages.xml 和 sitemap-blog.xml . Sitemap 類和 sitemaps 目錄根本沒有更改.
通知Google

當(dāng)你的sitemap變化的時(shí)候,你會想通知Google,以便讓它知道對你的站點(diǎn)進(jìn)行重新索引。 框架就提供了這樣的一個(gè)函數(shù): django.contrib.sitemaps.ping_google() 。

ping_google() 有一個(gè)可選的參數(shù) sitemap_url ,它應(yīng)該是你的站點(diǎn)地圖的URL絕對地址(例如:

如果不能夠確定你的sitemap URL, ping_google() 會引發(fā) django.contrib.sitemaps.SitemapNotFound 異常。

我們可以通過模型中的 save() 方法來調(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)度表來調(diào)用 ping_google() ,該方法使用Http直接請求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的使用詳解

    python異步編程之a(chǎn)syncio低階API的使用詳解

    asyncio中低階API的種類很多,涉及到開發(fā)的5個(gè)方面,這篇文章主要為大家詳細(xì)介紹了這些低階API的具體使用,感興趣的小伙伴可以學(xué)習(xí)一下
    2024-01-01
  • 使用Python做垃圾分類的原理及實(shí)例代碼附源碼

    使用Python做垃圾分類的原理及實(shí)例代碼附源碼

    這篇文章主要介紹了用Python做垃圾分類的實(shí)現(xiàn)原理,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下
    2019-07-07
  • 利用python numpy+matplotlib繪制股票k線圖的方法

    利用python numpy+matplotlib繪制股票k線圖的方法

    這篇文章主要介紹了利用python numpy+matplotlib繪制股票k線圖的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • 基于Python實(shí)現(xiàn)自動關(guān)機(jī)小工具

    基于Python實(shí)現(xiàn)自動關(guān)機(jī)小工具

    上班族經(jīng)常會遇到這樣情況,著急下班結(jié)果將關(guān)機(jī)誤點(diǎn)成重啟,或者臨近下班又通知開會,開完會已經(jīng)遲了還要去給電腦關(guān)機(jī)。今天使用PyQt5做了個(gè)自動關(guān)機(jī)的小工具,設(shè)置好關(guān)機(jī)時(shí)間然后直接提交即可,需要的可以參考一下
    2022-10-10
  • Python+Pygame繪制小球的實(shí)例詳解

    Python+Pygame繪制小球的實(shí)例詳解

    這篇文章主要為大家詳細(xì)介紹了如何利用Python?Pygame繪制小球(漸變大的小球、自由下落的小球、循環(huán)上下反彈的小球),感興趣的小伙伴可以了解一下
    2022-10-10
  • Python Social Auth構(gòu)建靈活而強(qiáng)大的社交登錄系統(tǒng)實(shí)例探究

    Python Social Auth構(gòu)建靈活而強(qiáng)大的社交登錄系統(tǒng)實(shí)例探究

    這篇文章主要為大家介紹了Python Social Auth構(gòu)建靈活而強(qiáng)大的社交登錄系統(tǒng)實(shí)例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-01-01
  • Python函數(shù)的作用域及內(nèi)置函數(shù)詳解

    Python函數(shù)的作用域及內(nèi)置函數(shù)詳解

    這篇文章主要介紹了python函數(shù)的作用域及內(nèi)置函數(shù)詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2021-09-09
  • Python中Django的路由配置詳解

    Python中Django的路由配置詳解

    這篇文章主要介紹了Python中Django的路由配置詳解,Python下有許多款不同的?Web?框架,Django是重量級選手中最有代表性的一位,許多成功的網(wǎng)站和APP都基于Django,需要的朋友可以參考下
    2023-07-07
  • django使用xlwt導(dǎo)出excel文件實(shí)例代碼

    django使用xlwt導(dǎo)出excel文件實(shí)例代碼

    這篇文章主要介紹了django使用xlwt導(dǎo)出excel文件實(shí)例代碼,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-02-02
  • 對python中UDP,socket的使用詳解

    對python中UDP,socket的使用詳解

    今天小編就為大家分享一篇對python中UDP,socket的使用詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08

最新評論