Ticket #16175: sitemaps-use-templateresponse.diff

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

Patch (with docs) against [16341]

  • docs/ref/contrib/sitemaps.txt

     
    312312            'template_name': 'custom_sitemap.html'
    313313        }),
    314314    )
     315   
     316.. versionadded:: 1.4
    315317
     318In addition, these views also return :class:`~django.template.response.TemplateResponse`
     319instances which allow you to easily customize the response data before rendering.
     320For more details, see the :doc:`TemplateResponse documentation </ref/template-response>`.
     321
    316322Pinging Google
    317323==============
    318324
  • django/contrib/sitemaps/views.py

     
    11from django.http import HttpResponse, Http404
    2 from django.template import loader
    32from django.contrib.sites.models import get_current_site
    43from django.core import urlresolvers
    54from django.utils.encoding import smart_str
    65from django.core.paginator import EmptyPage, PageNotAnInteger
     6from django.template.response import TemplateResponse
    77
    88def index(request, sitemaps, template_name='sitemap_index.xml'):
    99    current_site = get_current_site(request)
     
    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(template_name, {'sitemaps': sites})
    24     return HttpResponse(xml, mimetype='application/xml')
     23    return TemplateResponse(request, template_name, {'sitemaps': sites}, mimetype='application/xml')
    2524
    2625def sitemap(request, sitemaps, section=None, template_name='sitemap.xml'):
    2726    maps, urls = [], []
     
    4342            raise Http404("Page %s empty" % page)
    4443        except PageNotAnInteger:
    4544            raise Http404("No page '%s'" % page)
    46     xml = smart_str(loader.render_to_string(template_name, {'urlset': urls}))
    47     return HttpResponse(xml, mimetype='application/xml')
     45    return TemplateResponse(request, template_name, {'urlset': urls}, mimetype='application/xml')
Back to Top