Ticket #12669: direct_to_template.diff

File direct_to_template.diff, 2.3 KB (added by Kyle Dodson, 15 years ago)

Proposed change to direct_to_template generic view with docs change

  • django/views/generic/simple.py

     
    11from django.template import loader, RequestContext
    22from django.http import HttpResponse, HttpResponseRedirect, HttpResponsePermanentRedirect, HttpResponseGone
    33
    4 def direct_to_template(request, template, extra_context=None, mimetype=None, **kwargs):
     4def direct_to_template(request, template, extra_context=None, mimetype=None, response=HttpResponse, **kwargs):
    55    """
    66    Render a given template with any extra URL parameters in the context as
    77    ``{{ params }}``.
     
    1515            dictionary[key] = value
    1616    c = RequestContext(request, dictionary)
    1717    t = loader.get_template(template)
    18     return HttpResponse(t.render(c), mimetype=mimetype)
     18    return response(t.render(c), mimetype=mimetype)
    1919
    2020def redirect_to(request, url, permanent=True, **kwargs):
    2121    """
  • docs/ref/generic-views.txt

     
    5050    * ``mimetype``: The MIME type to use for the resulting document. Defaults
    5151      to the value of the ``DEFAULT_CONTENT_TYPE`` setting.
    5252
     53    * ``response``: The ``HttpResponse`` class to return. Defaults to the
     54      ``HttpResponse`` class.
     55
    5356**Example:**
    5457
    5558Given the following URL patterns::
     
    5760    urlpatterns = patterns('django.views.generic.simple',
    5861        (r'^foo/$',             'direct_to_template', {'template': 'foo_index.html'}),
    5962        (r'^foo/(?P<id>\d+)/$', 'direct_to_template', {'template': 'foo_detail.html'}),
     63        (r'^foo/bar/$',         'direct_to_template', {'template': 'foo_forbidden.html', 'response': HttpResponseForbidden}),
    6064    )
    6165
    6266... a request to ``/foo/`` would render the template ``foo_index.html``, and a
    6367request to ``/foo/15/`` would render the ``foo_detail.html`` with a context
    64 variable ``{{ params.id }}`` that is set to ``15``.
     68variable ``{{ params.id }}`` that is set to ``15``. A request to ``/foo/bar/``
     69would render the ``foo_forbidden.html`` with a 403 status code.
    6570
    6671``django.views.generic.simple.redirect_to``
    6772-------------------------------------------
Back to Top