diff --git a/django/contrib/sitemaps/tests/basic.py b/django/contrib/sitemaps/tests/basic.py index 775e0d9736..b675c3d6fb 100644 --- a/django/contrib/sitemaps/tests/basic.py +++ b/django/contrib/sitemaps/tests/basic.py @@ -37,9 +37,7 @@ class SitemapTests(TestCase): def test_simple_sitemap_index(self): "A simple sitemap index can be rendered" - # Retrieve the sitemap. response = self.client.get('/simple/index.xml') - # Check for all the important bits: self.assertEqual(response.content, """ %s/simple/sitemap-simple.xml @@ -48,9 +46,7 @@ class SitemapTests(TestCase): def test_simple_sitemap_custom_index(self): "A simple sitemap index can be rendered with a custom template" - # Retrieve the sitemap. response = self.client.get('/simple/custom-index.xml') - # Check for all the important bits: self.assertEqual(response.content, """ @@ -60,9 +56,7 @@ class SitemapTests(TestCase): def test_simple_sitemap(self): "A simple sitemap can be rendered" - # Retrieve the sitemap. response = self.client.get('/simple/sitemap.xml') - # Check for all the important bits: self.assertEqual(response.content, """ %s/location/%snever0.5 @@ -71,9 +65,7 @@ class SitemapTests(TestCase): def test_simple_custom_sitemap(self): "A simple sitemap can be rendered with a custom template" - # Retrieve the sitemap. response = self.client.get('/simple/custom-sitemap.xml') - # Check for all the important bits: self.assertEqual(response.content, """ @@ -98,13 +90,10 @@ class SitemapTests(TestCase): def test_generic_sitemap(self): "A minimal generic sitemap can be rendered" - # Retrieve the sitemap. response = self.client.get('/generic/sitemap.xml') - expected = '' for username in User.objects.values_list("username", flat=True): expected += "%s/users/%s/" % (self.base_url, username) - # Check for all the important bits: self.assertEqual(response.content, """ %s @@ -145,9 +134,7 @@ class SitemapTests(TestCase): # Make sure hitting the flatpages sitemap without the sites framework # installed doesn't raise an exception Site._meta.installed = False - # Retrieve the sitemap. response = self.client.get('/simple/sitemap.xml') - # Check for all the important bits: self.assertEqual(response.content, """ http://testserver/location/%snever0.5 @@ -182,3 +169,14 @@ class SitemapTests(TestCase): return isinstance(url['item'], User) item_in_url_info = all(map(is_user, user_sitemap.get_urls())) self.assertTrue(item_in_url_info) + + def test_cached_sitemap_index(self): + """ + Check that a cached sitemap index can be rendered (#2713). + """ + response = self.client.get('/cached/index.xml') + self.assertEqual(response.content, """ + +%s/cached/sitemap-simple.xml + +""" % self.base_url) diff --git a/django/contrib/sitemaps/tests/urls.py b/django/contrib/sitemaps/tests/urls.py index c264f940d2..d9ff7200d3 100644 --- a/django/contrib/sitemaps/tests/urls.py +++ b/django/contrib/sitemaps/tests/urls.py @@ -1,7 +1,8 @@ from datetime import datetime -from django.conf.urls import patterns -from django.contrib.sitemaps import Sitemap, GenericSitemap, FlatPageSitemap +from django.conf.urls import patterns, url +from django.contrib.sitemaps import Sitemap, GenericSitemap, FlatPageSitemap, views from django.contrib.auth.models import User +from django.views.decorators.cache import cache_page class SimpleSitemap(Sitemap): changefreq = "never" @@ -28,10 +29,17 @@ flatpage_sitemaps = { urlpatterns = patterns('django.contrib.sitemaps.views', (r'^simple/index\.xml$', 'index', {'sitemaps': simple_sitemaps}), - (r'^simple/custom-index\.xml$', 'index', {'sitemaps': simple_sitemaps, 'template_name': 'custom_sitemap_index.xml'}), - (r'^simple/sitemap-(?P
.+)\.xml$', 'sitemap', {'sitemaps': simple_sitemaps}), + (r'^simple/custom-index\.xml$', 'index', + {'sitemaps': simple_sitemaps, 'template_name': 'custom_sitemap_index.xml'}), + (r'^simple/sitemap-(?P
.+)\.xml$', 'sitemap', + {'sitemaps': simple_sitemaps}), (r'^simple/sitemap\.xml$', 'sitemap', {'sitemaps': simple_sitemaps}), - (r'^simple/custom-sitemap\.xml$', 'sitemap', {'sitemaps': simple_sitemaps, 'template_name': 'custom_sitemap.xml'}), + (r'^simple/custom-sitemap\.xml$', 'sitemap', + {'sitemaps': simple_sitemaps, 'template_name': 'custom_sitemap.xml'}), (r'^generic/sitemap\.xml$', 'sitemap', {'sitemaps': generic_sitemaps}), (r'^flatpages/sitemap\.xml$', 'sitemap', {'sitemaps': flatpage_sitemaps}), + url(r'^cached/index\.xml$', cache_page(1)(views.index), + {'sitemaps': simple_sitemaps, 'sitemap_url_name': 'cached_sitemap'}), + url(r'^cached/sitemap-(?P
.+)\.xml', cache_page(1)(views.sitemap), + {'sitemaps': simple_sitemaps}, name='cached_sitemap') ) diff --git a/django/contrib/sitemaps/views.py b/django/contrib/sitemaps/views.py index 14af5f1e4c..2760339f8b 100644 --- a/django/contrib/sitemaps/views.py +++ b/django/contrib/sitemaps/views.py @@ -4,7 +4,9 @@ from django.core.paginator import EmptyPage, PageNotAnInteger from django.http import Http404 from django.template.response import TemplateResponse -def index(request, sitemaps, template_name='sitemap_index.xml', mimetype='application/xml'): +def index(request, sitemaps, + template_name='sitemap_index.xml', mimetype='application/xml', + sitemap_url_name='django.contrib.sitemaps.views.sitemap'): current_site = get_current_site(request) sites = [] protocol = request.is_secure() and 'https' or 'http' @@ -14,14 +16,15 @@ def index(request, sitemaps, template_name='sitemap_index.xml', mimetype='applic pages = site().paginator.num_pages else: pages = site.paginator.num_pages - sitemap_url = urlresolvers.reverse('django.contrib.sitemaps.views.sitemap', kwargs={'section': section}) + sitemap_url = urlresolvers.reverse(sitemap_url_name, kwargs={'section': section}) sites.append('%s://%s%s' % (protocol, current_site.domain, sitemap_url)) if pages > 1: for page in range(2, pages+1): sites.append('%s://%s%s?p=%s' % (protocol, current_site.domain, sitemap_url, page)) return TemplateResponse(request, template_name, {'sitemaps': sites}, content_type=mimetype) -def sitemap(request, sitemaps, section=None, template_name='sitemap.xml', mimetype='application/xml'): +def sitemap(request, sitemaps, section=None, + template_name='sitemap.xml', mimetype='application/xml'): maps, urls = [], [] if section is not None: if section not in sitemaps: diff --git a/docs/ref/contrib/sitemaps.txt b/docs/ref/contrib/sitemaps.txt index f3ca8d1031..f4ae417666 100644 --- a/docs/ref/contrib/sitemaps.txt +++ b/docs/ref/contrib/sitemaps.txt @@ -293,11 +293,30 @@ 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 index will reflect that. -.. versionadded:: 1.3 +.. versionadded:: 1.4 + +If you are not using the vanilla sitemap view -- for example, if it is wrapped +with a caching decorator -- you must name your sitemap view and pass +``sitemap_url_name`` to the index view:: + + from django.contrib.sitemaps import views as sitemaps_views + from django.views.decorators.cache import cache_page + + urlpatterns = patterns('', + url(r'^sitemap.xml$', + cache_page(86400)(sitemaps_views.index), + {'sitemaps': sitemaps, 'sitemap_url_name': 'sitemaps'}), + url(r'^sitemap-(?P
.+)\.xml$', + cache_page(86400)(sitemaps_views.sitemap), + {'sitemaps': sitemaps}, name='sitemaps'), + ) + Template customization ====================== +.. versionadded:: 1.3 + 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::