Skip to content

Commit d0257a1

Browse files
committed
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
1 parent b3520da commit d0257a1

File tree

6 files changed

+92
-6
lines changed

6 files changed

+92
-6
lines changed

django/contrib/sitemaps/tests/basic.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from datetime import date
23
from django.conf import settings
34
from django.contrib.auth.models import User
@@ -16,12 +17,40 @@ class SitemapTests(TestCase):
1617
def setUp(self):
1718
self.old_USE_L10N = settings.USE_L10N
1819
self.old_Site_meta_installed = Site._meta.installed
20+
self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS
21+
settings.TEMPLATE_DIRS = (
22+
os.path.join(os.path.dirname(__file__), 'templates'),
23+
)
1924
# Create a user that will double as sitemap content
2025
User.objects.create_user('testuser', '[email protected]', 's3krit')
2126

2227
def tearDown(self):
2328
settings.USE_L10N = self.old_USE_L10N
2429
Site._meta.installed = self.old_Site_meta_installed
30+
settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS
31+
32+
def test_simple_sitemap_index(self):
33+
"A simple sitemap index can be rendered"
34+
# Retrieve the sitemap.
35+
response = self.client.get('/simple/index.xml')
36+
# Check for all the important bits:
37+
self.assertEquals(response.content, """<?xml version="1.0" encoding="UTF-8"?>
38+
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
39+
<sitemap><loc>http://example.com/simple/sitemap-simple.xml</loc></sitemap>
40+
</sitemapindex>
41+
""")
42+
43+
def test_simple_sitemap_custom_index(self):
44+
"A simple sitemap index can be rendered with a custom template"
45+
# Retrieve the sitemap.
46+
response = self.client.get('/simple/custom-index.xml')
47+
# Check for all the important bits:
48+
self.assertEquals(response.content, """<?xml version="1.0" encoding="UTF-8"?>
49+
<!-- This is a customised template -->
50+
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
51+
<sitemap><loc>http://example.com/simple/sitemap-simple.xml</loc></sitemap>
52+
</sitemapindex>
53+
""")
2554

2655
def test_simple_sitemap(self):
2756
"A simple sitemap can be rendered"
@@ -32,6 +61,18 @@ def test_simple_sitemap(self):
3261
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
3362
<url><loc>http://example.com/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url>
3463
</urlset>
64+
""" % date.today().strftime('%Y-%m-%d'))
65+
66+
def test_simple_custom_sitemap(self):
67+
"A simple sitemap can be rendered with a custom template"
68+
# Retrieve the sitemap.
69+
response = self.client.get('/simple/custom-sitemap.xml')
70+
# Check for all the important bits:
71+
self.assertEquals(response.content, """<?xml version="1.0" encoding="UTF-8"?>
72+
<!-- This is a customised template -->
73+
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
74+
<url><loc>http://example.com/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url>
75+
</urlset>
3576
""" % date.today().strftime('%Y-%m-%d'))
3677

3778
@skipUnless(settings.USE_I18N, "Internationalization is not enabled")
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- This is a customised template -->
3+
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
4+
{% spaceless %}
5+
{% for url in urlset %}
6+
<url>
7+
<loc>{{ url.location }}</loc>
8+
{% if url.lastmod %}<lastmod>{{ url.lastmod|date:"Y-m-d" }}</lastmod>{% endif %}
9+
{% if url.changefreq %}<changefreq>{{ url.changefreq }}</changefreq>{% endif %}
10+
{% if url.priority %}<priority>{{ url.priority }}</priority>{% endif %}
11+
</url>
12+
{% endfor %}
13+
{% endspaceless %}
14+
</urlset>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- This is a customised template -->
3+
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
4+
{% for location in sitemaps %}<sitemap><loc>{{ location }}</loc></sitemap>{% endfor %}
5+
</sitemapindex>

django/contrib/sitemaps/tests/urls.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ def items(self):
2727
}
2828

2929
urlpatterns = patterns('django.contrib.sitemaps.views',
30+
(r'^simple/index\.xml$', 'index', {'sitemaps': simple_sitemaps}),
31+
(r'^simple/custom-index\.xml$', 'index', {'sitemaps': simple_sitemaps, 'template_name': 'custom_sitemap_index.xml'}),
32+
(r'^simple/sitemap-(?P<section>.+)\.xml$', 'sitemap', {'sitemaps': simple_sitemaps}),
3033
(r'^simple/sitemap\.xml$', 'sitemap', {'sitemaps': simple_sitemaps}),
34+
(r'^simple/custom-sitemap\.xml$', 'sitemap', {'sitemaps': simple_sitemaps, 'template_name': 'custom_sitemap.xml'}),
3135
(r'^generic/sitemap\.xml$', 'sitemap', {'sitemaps': generic_sitemaps}),
3236
(r'^flatpages/sitemap\.xml$', 'sitemap', {'sitemaps': flatpage_sitemaps}),
3337
)

django/contrib/sitemaps/views.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from django.utils.encoding import smart_str
66
from django.core.paginator import EmptyPage, PageNotAnInteger
77

8-
def index(request, sitemaps):
8+
def index(request, sitemaps, template_name='sitemap_index.xml'):
99
current_site = get_current_site(request)
1010
sites = []
1111
protocol = request.is_secure() and 'https' or 'http'
@@ -20,10 +20,10 @@ def index(request, sitemaps):
2020
if pages > 1:
2121
for page in range(2, pages+1):
2222
sites.append('%s://%s%s?p=%s' % (protocol, current_site.domain, sitemap_url, page))
23-
xml = loader.render_to_string('sitemap_index.xml', {'sitemaps': sites})
23+
xml = loader.render_to_string(template_name, {'sitemaps': sites})
2424
return HttpResponse(xml, mimetype='application/xml')
2525

26-
def sitemap(request, sitemaps, section=None):
26+
def sitemap(request, sitemaps, section=None, template_name='sitemap.xml'):
2727
maps, urls = [], []
2828
if section is not None:
2929
if section not in sitemaps:
@@ -43,5 +43,5 @@ def sitemap(request, sitemaps, section=None):
4343
raise Http404("Page %s empty" % page)
4444
except PageNotAnInteger:
4545
raise Http404("No page '%s'" % page)
46-
xml = smart_str(loader.render_to_string('sitemap.xml', {'urlset': urls}))
46+
xml = smart_str(loader.render_to_string(template_name, {'urlset': urls}))
4747
return HttpResponse(xml, mimetype='application/xml')

docs/ref/contrib/sitemaps.txt

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,10 @@ references individual sitemap files, one per each section defined in your
279279

280280
Here's what the relevant URLconf lines would look like for the example above::
281281

282-
(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.index', {'sitemaps': sitemaps}),
283-
(r'^sitemap-(?P<section>.+)\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}),
282+
urlpatterns = patterns('django.contrib.sitemaps.views',
283+
(r'^sitemap\.xml$', 'index', {'sitemaps': sitemaps}),
284+
(r'^sitemap-(?P<section>.+)\.xml$', 'sitemap', {'sitemaps': sitemaps}),
285+
)
284286

285287
This will automatically generate a :file:`sitemap.xml` file that references both
286288
: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
291293
URLs. In this case, Django will automatically paginate the sitemap, and the
292294
index will reflect that.
293295

296+
.. versionadded:: 1.3
297+
298+
Template customization
299+
======================
300+
301+
If you wish to use a different template for each sitemap or sitemap index available on your site,
302+
you may specify it by passing a `template_name` parameter to the `sitemap` and `index` views via
303+
the URLconf::
304+
305+
urlpatterns = patterns('django.contrib.sitemaps.views',
306+
(r'^custom-sitemap\.xml$', 'index', {
307+
'sitemaps': sitemaps,
308+
'template_name': 'custom_sitemap.html'
309+
}),
310+
(r'^custom-sitemap-(?P<section>.+)\.xml$', 'sitemap', {
311+
'sitemaps': sitemaps,
312+
'template_name': 'custom_sitemap.html'
313+
}),
314+
)
315+
294316
Pinging Google
295317
==============
296318

0 commit comments

Comments
 (0)