From d0257a1558524158379bc2bf6bef2bc2a8811840 Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Sun, 12 Dec 2010 22:56:29 +0000 Subject: [PATCH] Fixed #14041 -- Added ability to override the template of the sitemaps views. Thanks, julien. git-svn-id: http://code.djangoproject.com/svn/django/trunk@14883 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/sitemaps/tests/basic.py | 41 +++++++++++++++++++ .../tests/templates/custom_sitemap.xml | 14 +++++++ .../tests/templates/custom_sitemap_index.xml | 5 +++ django/contrib/sitemaps/tests/urls.py | 4 ++ django/contrib/sitemaps/views.py | 8 ++-- docs/ref/contrib/sitemaps.txt | 26 +++++++++++- 6 files changed, 92 insertions(+), 6 deletions(-) create mode 100644 django/contrib/sitemaps/tests/templates/custom_sitemap.xml create mode 100644 django/contrib/sitemaps/tests/templates/custom_sitemap_index.xml diff --git a/django/contrib/sitemaps/tests/basic.py b/django/contrib/sitemaps/tests/basic.py index 3070f21bc1..3874ac740f 100644 --- a/django/contrib/sitemaps/tests/basic.py +++ b/django/contrib/sitemaps/tests/basic.py @@ -1,3 +1,4 @@ +import os from datetime import date from django.conf import settings from django.contrib.auth.models import User @@ -16,12 +17,40 @@ class SitemapTests(TestCase): def setUp(self): self.old_USE_L10N = settings.USE_L10N self.old_Site_meta_installed = Site._meta.installed + self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS + settings.TEMPLATE_DIRS = ( + os.path.join(os.path.dirname(__file__), 'templates'), + ) # Create a user that will double as sitemap content User.objects.create_user('testuser', 'test@example.com', 's3krit') def tearDown(self): settings.USE_L10N = self.old_USE_L10N Site._meta.installed = self.old_Site_meta_installed + settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS + + 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.assertEquals(response.content, """ + +http://example.com/simple/sitemap-simple.xml + +""") + + 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.assertEquals(response.content, """ + + +http://example.com/simple/sitemap-simple.xml + +""") def test_simple_sitemap(self): "A simple sitemap can be rendered" @@ -32,6 +61,18 @@ class SitemapTests(TestCase): http://example.com/location/%snever0.5 +""" % date.today().strftime('%Y-%m-%d')) + + 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.assertEquals(response.content, """ + + +http://example.com/location/%snever0.5 + """ % date.today().strftime('%Y-%m-%d')) @skipUnless(settings.USE_I18N, "Internationalization is not enabled") diff --git a/django/contrib/sitemaps/tests/templates/custom_sitemap.xml b/django/contrib/sitemaps/tests/templates/custom_sitemap.xml new file mode 100644 index 0000000000..594aef1a3e --- /dev/null +++ b/django/contrib/sitemaps/tests/templates/custom_sitemap.xml @@ -0,0 +1,14 @@ + + + +{% spaceless %} +{% for url in urlset %} + + {{ url.location }} + {% if url.lastmod %}{{ url.lastmod|date:"Y-m-d" }}{% endif %} + {% if url.changefreq %}{{ url.changefreq }}{% endif %} + {% if url.priority %}{{ url.priority }}{% endif %} + +{% endfor %} +{% endspaceless %} + diff --git a/django/contrib/sitemaps/tests/templates/custom_sitemap_index.xml b/django/contrib/sitemaps/tests/templates/custom_sitemap_index.xml new file mode 100644 index 0000000000..406c6b7606 --- /dev/null +++ b/django/contrib/sitemaps/tests/templates/custom_sitemap_index.xml @@ -0,0 +1,5 @@ + + + +{% for location in sitemaps %}{{ location }}{% endfor %} + diff --git a/django/contrib/sitemaps/tests/urls.py b/django/contrib/sitemaps/tests/urls.py index 6cdba36b02..03b060df7a 100644 --- a/django/contrib/sitemaps/tests/urls.py +++ b/django/contrib/sitemaps/tests/urls.py @@ -27,7 +27,11 @@ 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/sitemap\.xml$', 'sitemap', {'sitemaps': simple_sitemaps}), + (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}), ) diff --git a/django/contrib/sitemaps/views.py b/django/contrib/sitemaps/views.py index b7a96e12aa..83094402c6 100644 --- a/django/contrib/sitemaps/views.py +++ b/django/contrib/sitemaps/views.py @@ -5,7 +5,7 @@ from django.core import urlresolvers from django.utils.encoding import smart_str from django.core.paginator import EmptyPage, PageNotAnInteger -def index(request, sitemaps): +def index(request, sitemaps, template_name='sitemap_index.xml'): current_site = get_current_site(request) sites = [] protocol = request.is_secure() and 'https' or 'http' @@ -20,10 +20,10 @@ def index(request, sitemaps): if pages > 1: for page in range(2, pages+1): sites.append('%s://%s%s?p=%s' % (protocol, current_site.domain, sitemap_url, page)) - xml = loader.render_to_string('sitemap_index.xml', {'sitemaps': sites}) + xml = loader.render_to_string(template_name, {'sitemaps': sites}) return HttpResponse(xml, mimetype='application/xml') -def sitemap(request, sitemaps, section=None): +def sitemap(request, sitemaps, section=None, template_name='sitemap.xml'): maps, urls = [], [] if section is not None: if section not in sitemaps: @@ -43,5 +43,5 @@ def sitemap(request, sitemaps, section=None): raise Http404("Page %s empty" % page) except PageNotAnInteger: raise Http404("No page '%s'" % page) - xml = smart_str(loader.render_to_string('sitemap.xml', {'urlset': urls})) + xml = smart_str(loader.render_to_string(template_name, {'urlset': urls})) return HttpResponse(xml, mimetype='application/xml') diff --git a/docs/ref/contrib/sitemaps.txt b/docs/ref/contrib/sitemaps.txt index b58b55cab4..a9b1d08f66 100644 --- a/docs/ref/contrib/sitemaps.txt +++ b/docs/ref/contrib/sitemaps.txt @@ -279,8 +279,10 @@ references individual sitemap files, one per each section defined in your Here's what the relevant URLconf lines would look like for the example above:: - (r'^sitemap\.xml$', 'django.contrib.sitemaps.views.index', {'sitemaps': sitemaps}), - (r'^sitemap-(?P
.+)\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}), + urlpatterns = patterns('django.contrib.sitemaps.views', + (r'^sitemap\.xml$', 'index', {'sitemaps': sitemaps}), + (r'^sitemap-(?P
.+)\.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 @@ -291,6 +293,26 @@ 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 + +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:: + + urlpatterns = patterns('django.contrib.sitemaps.views', + (r'^custom-sitemap\.xml$', 'index', { + 'sitemaps': sitemaps, + 'template_name': 'custom_sitemap.html' + }), + (r'^custom-sitemap-(?P
.+)\.xml$', 'sitemap', { + 'sitemaps': sitemaps, + 'template_name': 'custom_sitemap.html' + }), + ) + Pinging Google ==============