除了普通的 sitemap.xml 之外,我还尝试构建一个 Google 新闻站点地图,以避免在我的网址中添加额外的数字字符。
我已经使用 Django 的 contrib 系统构建了 sitemap.xml,它运行良好,但是我无法将上下文传递给(未经验证的)补丁更新框架以生成 news_sitemap.xml。
这是我集成的补丁:http ://code.djangoproject.com/ticket/10907 ,但上下文没有通过。我认为问题在于我在views.py中用于构建对象的格式。
我运行的代码:
视图.py
from django.template import RequestContext
from django.shortcuts import render_to_response
from basic.blog.models import Post
from pages.models import Page
from datetime import date, datetime
from django.contrib.sitemaps import Sitemap, NewsSitemap
'''Builds the sitemap.xml section for all news posts.'''
class PostSitemap(Sitemap):
changefreq = "daily"
priority = 0.3
def items(self):
return Post.objects.published()
def lastmod(self, obj):
return obj.modified
'''Builds the sitemap.xml section for all main pages.'''
class PageSitemap(Sitemap):
changefreq = "daily"
priority = 0.8
def items(self):
return Page.objects.filter(status=1)
def lastmod(self, obj):
return obj.last_modification_date
'''Builds the news_sitemap.xml from blog module.'''
class SiteappNews(Sitemap):
def items(self):
return Post.objects.published()
def publication_date(self, obj):
return obj.publish
网址.py
from django.conf.urls.defaults import *
from django.contrib.sitemaps import Sitemap, FlatPageSitemap, NewsSitemap
from siteapp.views import homepage, news_homepage, qc_contact, PostSitemap, PageSitemap, SiteappNews
from basic.blog.feeds import *
from basic.blog.models import Post
from pages.models import Page
''' Enables Django Admin.'''
from django.contrib import admin
admin.autodiscover()
'''Add Feeds functionality.'''
feeds = {
'latest': BlogPostsFeed,
}
'''http://docs.djangoproject.com/en/1.0/ref/contrib/sitemaps/'''
sitemaps = {
'pagesitemap': PageSitemap,
'postsitemap': PostSitemap,
'flatpages': FlatPageSitemap,
}
news_sitemaps = {
'newssitemap': NewsSitemap,
}
urlpatterns = patterns('',
(r'^admin/doc/', include('django.contrib.admindocs.urls')),
(r'^admin/(.*)', admin.site.root),
(r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}),
(r'^news_sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': news_sitemaps, 'template': 'news_sitemap.xml'}),
模板输出只是包装器。尽管应用的补丁可能存在问题,但我认为我遗漏了一些明显的东西。这是相关的代码:
在站点地图中 contrib init .py
class NewsSitemap(Sitemap):
# This limit is defined by Google. See the index documentation at
# http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=74288
limit = 1000
def get_url_info(self, item, current_site):
url_info = super(NewsSitemap, self).get_url_info(item, current_site)
url_info.update({
'publication_date': self._get('publication_date', item, None),
'keywords': self._get('keywords', item, None),
})
return url_info