Django谷歌新闻Sitemap.xml

时间:2009-08-19 09:38:07

标签: django sitemap google-news

除了普通的sitemap.xml之外,我正在尝试构建Google新闻站点地图,以避免在我的网址中添加其他数字字符。

我使用Django的contrib系统构建了sitemap.xml,它工作得很好,但我无法将上下文传递给(未经验证的)补丁,更新框架以生成news_sitemap.xml。

这是我整合的补丁:http://code.djangoproject.com/ticket/10907,但上下文没有通过。我认为问题在于我用于在views.py中构建对象的格式。

我运行的代码:

views.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

urls.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'}),

模板只输出包装。虽然应用补丁可能存在问题,但我认为我遗漏了一些明显的东西。以下是相关代码:

在sitemaps 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

1 个答案:

答案 0 :(得分:1)

经过一番挖掘后,将自己排除在外。

将urls.py行更改为:

news_sitemaps = {
    'newssitemap': SiteappNews,
}

并更改了views.py中的代码,以便从自定义模块中构建相关的Google新闻字段。

你的未来读者会有所不同(你好!),但它会是这样的:

class SiteappNews(Sitemap):
    def items(self):
        return Post.objects.published()
    def publication_date(self, obj):
        return obj.publish
    def keywords(self, obj):
        return obj.tags

检查您的模型的SQL字段是否为“发布”,“标签”等的正确数据。