Ticket #12669: direct_to_template.diff
File direct_to_template.diff, 2.3 KB (added by , 15 years ago) |
---|
-
django/views/generic/simple.py
1 1 from django.template import loader, RequestContext 2 2 from django.http import HttpResponse, HttpResponseRedirect, HttpResponsePermanentRedirect, HttpResponseGone 3 3 4 def direct_to_template(request, template, extra_context=None, mimetype=None, **kwargs):4 def direct_to_template(request, template, extra_context=None, mimetype=None, response=HttpResponse, **kwargs): 5 5 """ 6 6 Render a given template with any extra URL parameters in the context as 7 7 ``{{ params }}``. … … 15 15 dictionary[key] = value 16 16 c = RequestContext(request, dictionary) 17 17 t = loader.get_template(template) 18 return HttpResponse(t.render(c), mimetype=mimetype)18 return response(t.render(c), mimetype=mimetype) 19 19 20 20 def redirect_to(request, url, permanent=True, **kwargs): 21 21 """ -
docs/ref/generic-views.txt
50 50 * ``mimetype``: The MIME type to use for the resulting document. Defaults 51 51 to the value of the ``DEFAULT_CONTENT_TYPE`` setting. 52 52 53 * ``response``: The ``HttpResponse`` class to return. Defaults to the 54 ``HttpResponse`` class. 55 53 56 **Example:** 54 57 55 58 Given the following URL patterns:: … … 57 60 urlpatterns = patterns('django.views.generic.simple', 58 61 (r'^foo/$', 'direct_to_template', {'template': 'foo_index.html'}), 59 62 (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}), 60 64 ) 61 65 62 66 ... a request to ``/foo/`` would render the template ``foo_index.html``, and a 63 67 request to ``/foo/15/`` would render the ``foo_detail.html`` with a context 64 variable ``{{ params.id }}`` that is set to ``15``. 68 variable ``{{ params.id }}`` that is set to ``15``. A request to ``/foo/bar/`` 69 would render the ``foo_forbidden.html`` with a 403 status code. 65 70 66 71 ``django.views.generic.simple.redirect_to`` 67 72 -------------------------------------------