Commit b3d20916 authored by Russell Keith-Magee's avatar Russell Keith-Magee
Browse files

Fixed #12816 -- Added a render() shortcut.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15008 bcc190cf-cafb-0310-a4f2-bffc1f526a37
parent d4ef8414
Loading
Loading
Loading
Loading
+22 −10
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ of MVC. In other words, these functions/classes introduce controlled coupling
for convenience's sake.
"""

from django.template import loader
from django.template import loader, RequestContext
from django.http import HttpResponse, Http404
from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect
from django.db.models.manager import Manager
@@ -19,6 +19,17 @@ def render_to_response(*args, **kwargs):
    httpresponse_kwargs = {'mimetype': kwargs.pop('mimetype', None)}
    return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)

def render(request, *args, **kwargs):
    """
    Returns a HttpResponse whose content is filled with the result of calling
    django.template.loader.render_to_string() with the passed arguments.
    Uses a RequestContext by default.
    """
    httpresponse_kwargs = {'mimetype': kwargs.pop('mimetype', None)}
    kwargs['context_instance'] = kwargs.get('context_instance', RequestContext(request))
    return HttpResponse(loader.render_to_string(*args, **kwargs),
                        **httpresponse_kwargs)

def redirect(to, *args, **kwargs):
    """
    Returns an HttpResponseRedirect to the apropriate URL for the arguments
@@ -102,3 +113,4 @@ def get_list_or_404(klass, *args, **kwargs):
    if not obj_list:
        raise Http404('No %s matches the given query.' % queryset.model._meta.object_name)
    return obj_list
+5 −0
Original line number Diff line number Diff line
@@ -215,6 +215,11 @@ requests. These include:
      making it easier to write simple template tags that require
      access to template context.

    * A new :meth:`~django.shortcuts.render()` shortcut -- an
      alternative to :meth:`~django.shortcuts.render_to_response()`
      providing a :class:`~django.template.RequestContext` by
      default.

.. _HTTPOnly: http://www.owasp.org/index.php/HTTPOnly

.. _backwards-incompatible-changes-1.3:
+64 −0
Original line number Diff line number Diff line
@@ -12,6 +12,70 @@ The package ``django.shortcuts`` collects helper functions and classes that
"span" multiple levels of MVC. In other words, these functions/classes
introduce controlled coupling for convenience's sake.

``render``
==========

.. function:: render(request, template[, dictionary][, context_instance][, mimetype])

   Combines a given template with a given context dictionary and returns an
   :class:`~django.http.HttpResponse` object with that rendered text.

   :func:`render()` is the same as a call to
   :func:`render_to_response()` with a context_instance argument that
   that forces the use of a :class:`RequestContext`.

Required arguments
------------------

``request``
    The request object used to generate this response.

``template``
    The full name of a template to use or sequence of template names.

Optional arguments
------------------

``dictionary``
    A dictionary of values to add to the template context. By default, this
    is an empty dictionary. If a value in the dictionary is callable, the
    view will call it just before rendering the template.

``context_instance``
    The context instance to render the template with. By default, the template
    will be rendered with a ``RequestContext`` instance (filled with values from
    ``request`` and ```dictionary``).

``mimetype``
    The MIME type to use for the resulting document. Defaults to the value of
    the :setting:`DEFAULT_CONTENT_TYPE` setting.

Example
-------

The following example renders the template ``myapp/index.html`` with the
MIME type ``application/xhtml+xml``::

    from django.shortcuts import render_to_response

    def my_view(request):
        # View code here...
        return render_to_response('myapp/index.html', {"foo": "bar"},
            mimetype="application/xhtml+xml")

This example is equivalent to::

    from django.http import HttpResponse
    from django.template import Context, loader

    def my_view(request):
        # View code here...
        t = loader.get_template('myapp/template.html')
        c = RequestContext(request, {'foo': 'bar'})
        return HttpResponse(t.render(c),
            mimetype="application/xhtml+xml")


``render_to_response``
======================

+1 −1
Original line number Diff line number Diff line
{{ foo }}.{{ bar }}.{{ baz }}.{{ processors }}
{{ foo }}.{{ bar }}.{{ baz }}.{{ STATIC_URL }}
+1 −0
Original line number Diff line number Diff line
@@ -5,5 +5,6 @@ from generic.date_based import *
from generic.object_list import *
from generic.simple import *
from i18n import *
from shortcuts import *
from specials import *
from static import *
Loading