2008-06-21
djangoのsitemapフレームワークの使い方 その1
djangoに組み込まれているsitemapフレームワークを使うと、簡単にsitemapを使ったSEO対策を実現できます。
今回はこのsitemapフレームワークについて調べてみました。
sitemap配信の準備
sitemapsフレームワークを利用する準備として、まずsettings.pyのINSTALED_APPSに以下の記述を行います。
INSTALLED_APPS = (
...
...
'django.contrib.sitemaps',
)
次にurls.pyに以下の記述を行ってください。
from contents.sitemaps import * # sitemapとSitemapのサブクラスのマッピングを定義する sitemaps = {'site':TestSitemap} urlpatterns = patterns('', ... ... (r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap',{'sitemaps' : sitemaps}), )
次にsitemaps.pyにSitemapのサブクラスを定義します。
from django.contrib.sitemaps import Sitemap from contents.models import * class TestSitemap(Sitemap): def items(self): return Test.objects.all()[:2] def lastmod(self, obj): return obj.update_date def location(self, obj): return "/News/" + str(obj.id) + "/" def priority(self, obj): return 0.5 def changefreq(self, obj): return "weekly"
これだけで、サイト全体のsitemapが配信可能になりました。
sitemapの配信
http://<サーバー名>/sitemap.xmlから実際に配信されるsitemap.xmlは、次のようになります。
<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>http://sample.com/News/1/</loc> <lastmod>2008-06-21</lastmod> <changefreq>weekly</changefreq> <priority>0.5</priority> </url> <url> <loc>http://sample.com/News/2/</loc> <lastmod>2008-06-21</lastmod> <changefreq>weekly</changefreq> <priority>0.5</priority> </url> </urlset>
sitemapに設定できるプロパティ一覧
Sitemapのサブクラスには、以下のプロパティを定義できます。
なおitems以外はフィールドとして定義することが可能で、固定値を返す場合に利用できます。
items (必須)
sitemapのurlの単位となるオブジェクトのリストを返します。
location (オプション)
urlの詳細位置を表し、sitemapのloc要素にマッピングされる内容を返します。
lastmod (オプション)
urlの最終更新日を表し、sitemapのlastmod要素にマッピングされる内容を返します。
changefreq (オプション)
urlの更新頻度を表し、sitemapのchangefreq要素にマッピングされる内容を返します。
設定できる値は、以下のいずれかになります。
・always ・hourly ・daily ・weekly ・monthly ・yearly ・never
priority (オプション)
urlの優先度を表し、sitemapのpriority要素にマッピングされる内容を返します。
値は0.0〜1.0の間で設定され、デフォルト値は0.5になっています。
トラックバック - http://d.hatena.ne.jp/aqvi/20080621/1214025153
リンク元
- 9 http://reader.livedoor.com/reader/
- 7 http://www.google.com/reader/view/
- 6 http://r.hatena.ne.jp/esper/知り合い/
- 6 http://www.google.co.jp/reader/view/?hl=ja&tab=wy
- 5 http://d.hatena.ne.jp/keyworddiary/Python
- 4 http://d.hatena.ne.jp/asin/4274067149
- 4 http://d.hatena.ne.jp/johzan/20080622/1214067499
- 4 http://d.hatena.ne.jp/johzan/mobile?date=20080622§ion=1214067499
- 3 http://d.hatena.ne.jp/asin/4894714337
- 3 http://d.hatena.ne.jp/keyword/django