Django Google News Sitemap.xml

拥有回忆 提交于 2020-07-20 04:21:48

问题


I'm trying to build out a Google News Sitemap in addition to a normal sitemap.xml to avoid having to put additional numerical characters in my URLs.

I've build out the sitemap.xml using Django's contrib system and it works great, but I'm having trouble passing the context to the (unverified) patch updating the framework to gererate a news_sitemap.xml.

This is the patch I've integrated: http://code.djangoproject.com/ticket/10907, but the context isn't passing. I think the issue is with the format I'm using to build the object in my views.py.

The code I have running:

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

The template outputs just wrapper. I'm missing something obvious i think, though there might be an issue with the patch applied. Here's the relevant code for that:

Within 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:


Sorted this one out myself after a little digging.

Changed the urls.py lines to:

news_sitemaps = {
    'newssitemap': SiteappNews,
}

And changed the code in views.py to build out the relevant Google News fields from the custom module.

Yours will vary future reader (hello!), but it will be something like:

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

check your SQL fields for your models to sub in the correct data for 'publish', 'tags', etc.



来源:https://stackoverflow.com/questions/1298797/django-google-news-sitemap-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!