Code

Ticket #16175: sitemaps-use-templateresponse.diff

File sitemaps-use-templateresponse.diff, 2.2 KB (added by mat, 3 years ago)

Patch (with docs) against [16341]

Line 
1Index: docs/ref/contrib/sitemaps.txt
2===================================================================
3--- docs/ref/contrib/sitemaps.txt       (revision 16338)
4+++ docs/ref/contrib/sitemaps.txt       (working copy)
5@@ -312,7 +312,13 @@
6             'template_name': 'custom_sitemap.html'
7         }),
8     )
9+   
10+.. versionadded:: 1.4
11 
12+In addition, these views also return :class:`~django.template.response.TemplateResponse`
13+instances which allow you to easily customize the response data before rendering.
14+For more details, see the :doc:`TemplateResponse documentation </ref/template-response>`.
15+
16 Pinging Google
17 ==============
18 
19Index: django/contrib/sitemaps/views.py
20===================================================================
21--- django/contrib/sitemaps/views.py    (revision 16338)
22+++ django/contrib/sitemaps/views.py    (working copy)
23@@ -1,9 +1,9 @@
24 from django.http import HttpResponse, Http404
25-from django.template import loader
26 from django.contrib.sites.models import get_current_site
27 from django.core import urlresolvers
28 from django.utils.encoding import smart_str
29 from django.core.paginator import EmptyPage, PageNotAnInteger
30+from django.template.response import TemplateResponse
31 
32 def index(request, sitemaps, template_name='sitemap_index.xml'):
33     current_site = get_current_site(request)
34@@ -20,8 +20,7 @@
35         if pages > 1:
36             for page in range(2, pages+1):
37                 sites.append('%s://%s%s?p=%s' % (protocol, current_site.domain, sitemap_url, page))
38-    xml = loader.render_to_string(template_name, {'sitemaps': sites})
39-    return HttpResponse(xml, mimetype='application/xml')
40+    return TemplateResponse(request, template_name, {'sitemaps': sites}, mimetype='application/xml')
41 
42 def sitemap(request, sitemaps, section=None, template_name='sitemap.xml'):
43     maps, urls = [], []
44@@ -43,5 +42,4 @@
45             raise Http404("Page %s empty" % page)
46         except PageNotAnInteger:
47             raise Http404("No page '%s'" % page)
48-    xml = smart_str(loader.render_to_string(template_name, {'urlset': urls}))
49-    return HttpResponse(xml, mimetype='application/xml')
50+    return TemplateResponse(request, template_name, {'urlset': urls}, mimetype='application/xml')