Fixed #10907, #14190 and #15829 -- Pass item to sitemaps template to allow further customization like Google News enabled sitemaps. Thanks, manfre and lakinwecker.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16474 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jannis Leidel 2011-06-28 10:16:34 +00:00
parent dbffffa7dc
commit 3fe0c69332
4 changed files with 80 additions and 12 deletions

View File

@ -76,10 +76,11 @@ class Sitemap(object):
loc = "http://%s%s" % (site.domain, self.__get('location', item))
priority = self.__get('priority', item, None)
url_info = {
'item': item,
'location': loc,
'lastmod': self.__get('lastmod', item, None),
'changefreq': self.__get('changefreq', item, None),
'priority': str(priority is not None and priority or '')
'priority': str(priority is not None and priority or ''),
}
urls.append(url_info)
return urls

View File

@ -2,7 +2,7 @@ import os
from datetime import date
from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.sitemaps import Sitemap
from django.contrib.sitemaps import Sitemap, GenericSitemap
from django.contrib.sites.models import Site
from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase
@ -171,3 +171,14 @@ class SitemapTests(TestCase):
"""
Site._meta.installed = False
self.assertRaises(ImproperlyConfigured, Sitemap().get_urls)
def test_sitemap_item(self):
"""
Check to make sure that the raw item is included with each
Sitemap.get_url() url result.
"""
user_sitemap = GenericSitemap({'queryset': User.objects.all()})
def is_user(url):
return isinstance(url['item'], User)
item_in_url_info = all(map(is_user, user_sitemap.get_urls()))
self.assertTrue(item_in_url_info)

View File

@ -36,9 +36,8 @@ def sitemap(request, sitemaps, section=None, template_name='sitemap.xml'):
for site in maps:
try:
if callable(site):
urls.extend(site().get_urls(page=page, site=current_site))
else:
urls.extend(site.get_urls(page=page, site=current_site))
site = site()
urls.extend(site.get_urls(page=page, site=current_site))
except EmptyPage:
raise Http404("Page %s empty" % page)
except PageNotAnInteger:

View File

@ -284,10 +284,10 @@ Here's what the relevant URLconf lines would look like for the example above::
(r'^sitemap-(?P<section>.+)\.xml$', 'sitemap', {'sitemaps': sitemaps}),
)
This will automatically generate a :file:`sitemap.xml` file that references both
:file:`sitemap-flatpages.xml` and :file:`sitemap-blog.xml`. The
:class:`~django.contrib.sitemaps.Sitemap` classes and the :data:`sitemaps` dict
don't change at all.
This will automatically generate a :file:`sitemap.xml` file that references
both :file:`sitemap-flatpages.xml` and :file:`sitemap-blog.xml`. The
:class:`~django.contrib.sitemaps.Sitemap` classes and the :data:`sitemaps`
dict don't change at all.
You should create an index file if one of your sitemaps has more than 50,000
URLs. In this case, Django will automatically paginate the sitemap, and the
@ -298,9 +298,9 @@ index will reflect that.
Template customization
======================
If you wish to use a different template for each sitemap or sitemap index available on your site,
you may specify it by passing a `template_name` parameter to the `sitemap` and `index` views via
the URLconf::
If you wish to use a different template for each sitemap or sitemap index
available on your site, you may specify it by passing a ``template_name``
parameter to the ``sitemap`` and ``index`` views via the URLconf::
urlpatterns = patterns('django.contrib.sitemaps.views',
(r'^custom-sitemap\.xml$', 'index', {
@ -313,6 +313,63 @@ the URLconf::
}),
)
Context variables
------------------
When customizing the templates for the :func:`~django.contrib.sitemaps.views.index`
and ~:func:`django.contrib.sitemaps.views.sitemaps` views, you can rely on the
following context variables.
Index
-----
The variable :data:`sitemaps` is a list of absolute URLs to each of the sitemaps.
Sitemap
-------
The variable :data:`urlset` is a list of URLs that should appear in the
sitemap. Each URL exposes attributes as defined in the
:class:`~django.contrib.sitemaps.Sitemap` class:
- ``changefreq``
- ``item``
- ``lastmod``
- ``location``
- ``priority``
.. versionadded:: 1.4
The ``item`` attribute has been added for each URL to allow more flexible
customization of the templates, such as `Google news sitemaps`_. Assuming
Sitemap's :attr:`~Sitemap.items()` would return a list of items with
``publication_data`` and a ``tags`` field something like this would
generate a Google News compatible sitemap:
.. code-block:: xml+django
<?xml version="1.0" encoding="UTF-8"?>
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">
{% spaceless %}
{% for url in urlset %}
<url>
<loc>{{ url.location }}</loc>
{% if url.lastmod %}<lastmod>{{ url.lastmod|date:"Y-m-d" }}</lastmod>{% endif %}
{% if url.changefreq %}<changefreq>{{ url.changefreq }}</changefreq>{% endif %}
{% if url.priority %}<priority>{{ url.priority }}</priority>{% endif %}
<news:news>
{% if url.item.publication_date %}<news:publication_date>{{ url.item.publication_date|date:"Y-m-d" }}</news:publication_date>{% endif %}
{% if url.item.tags %}<news:keywords>{{ url.item.tags }}</news:keywords>{% endif %}
</news:news>
</url>
{% endfor %}
{% endspaceless %}
</urlset>
.. _`Google news sitemaps`: http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=74288
Pinging Google
==============