1. Mikhail Korobov
  2. django2

Commits

Mikhail Korobov  committed 5736a31

render shortcut implemented as TemplateResponse

  • Participants
  • Parent commits 8343c0d
  • Branches default

Comments (0)

Files changed (22)

File django/contrib/messages/middleware.py

View file
  • Ignore whitespace
 from django.conf import settings
 from django.contrib.messages.storage import default_storage
 
-
 class MessageMiddleware(object):
     """
     Middleware that handles temporary messages.

File django/contrib/messages/tests/base.py

View file
  • Ignore whitespace
         storage = self.get_storage()
         self.assertFalse(storage.added_new)
         storage.add(constants.INFO, 'Test message 1')
-        self.assert_(storage.added_new)
+        self.assertTrue(storage.added_new)
         storage.add(constants.INFO, 'Test message 2', extra_tags='tag')
         self.assertEqual(len(storage), 2)
 
             for msg in data['messages']:
                 self.assertContains(response, msg)
 
+    def test_with_template_response(self):
+        settings.MESSAGE_LEVEL = constants.DEBUG
+        data = {
+            'messages': ['Test message %d' % x for x in xrange(10)],
+        }
+        show_url = reverse(self.urls + '.show_template_response')
+
+        for level in self.levels.keys():
+            add_url = reverse(self.urls + '.add_template_response',
+                              args=(level,))
+            response = self.client.post(add_url, data, follow=True)
+            self.assertRedirects(response, show_url)
+            self.assertTrue('messages' in response.context)
+            for msg in data['messages']:
+                self.assertContains(response, msg)
+
+            # there shouldn't be any messages on second GET request
+            response = self.client.get(show_url)
+            for msg in data['messages']:
+                self.assertNotContains(response, msg)
+
     def test_multiple_posts(self):
         """
         Tests that messages persist properly when multiple POSTs are made

File django/contrib/messages/tests/urls.py

View file
  • Ignore whitespace
 from django.contrib import messages
 from django.core.urlresolvers import reverse
 from django.http import HttpResponseRedirect, HttpResponse
-from django.shortcuts import render_to_response
+from django.shortcuts import render_to_response, redirect
 from django.template import RequestContext, Template
+from django.template.response import TemplateResponse
 
+TEMPLATE = """{% if messages %}
+<ul class="messages">
+    {% for message in messages %}
+    <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>
+        {{ message }}
+    </li>
+    {% endfor %}
+</ul>
+{% endif %}
+"""
 
 def add(request, message_type):
     # don't default to False here, because we want to test that it defaults
                                             fail_silently=fail_silently)
         else:
             getattr(messages, message_type)(request, msg)
+
     show_url = reverse('django.contrib.messages.tests.urls.show')
     return HttpResponseRedirect(show_url)
 
+def add_template_response(request, message_type):
+    for msg in request.POST.getlist('messages'):
+        getattr(messages, message_type)(request, msg)
+    return redirect('django.contrib.messages.tests.urls.show_template_response')
 
 def show(request):
-    t = Template("""{% if messages %}
-<ul class="messages">
-    {% for message in messages %}
-    <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>
-        {{ message }}
-    </li>
-    {% endfor %}
-</ul>
-{% endif %}""")
+    t = Template(TEMPLATE)
     return HttpResponse(t.render(RequestContext(request)))
 
+def show_template_response(request):
+    return TemplateResponse(request, Template(TEMPLATE))
 
 urlpatterns = patterns('',
     ('^add/(debug|info|success|warning|error)/$', add),
     ('^show/$', show),
+    ('^add-template-response/(debug|info|success|warning|error)/$', add_template_response),
+    ('^show-template-response/$', show_template_response),
 )

File django/core/handlers/base.py

View file
  • Ignore whitespace
 from django.utils.encoding import force_unicode
 from django.utils.importlib import import_module
 from django.utils.log import getLogger
+from django.template.response import bake
 
 logger = getLogger('django.request')
 
     def __init__(self):
         self._request_middleware = self._view_middleware = self._response_middleware = self._exception_middleware = None
 
+
     def load_middleware(self):
         """
         Populate middleware lists from settings.MIDDLEWARE_CLASSES.
         from django.conf import settings
         from django.core import exceptions
         self._view_middleware = []
+        self._template_response_middleware = []
         self._response_middleware = []
         self._exception_middleware = []
 
         request_middleware = []
         for middleware_path in settings.MIDDLEWARE_CLASSES:
             try:
-                dot = middleware_path.rindex('.')
-            except ValueError:
-                raise exceptions.ImproperlyConfigured('%s isn\'t a middleware module' % middleware_path)
-            mw_module, mw_classname = middleware_path[:dot], middleware_path[dot+1:]
-            try:
-                mod = import_module(mw_module)
-            except ImportError, e:
-                raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
-            try:
-                mw_class = getattr(mod, mw_classname)
-            except AttributeError:
-                raise exceptions.ImproperlyConfigured('Middleware module "%s" does not define a "%s" class' % (mw_module, mw_classname))
-
-            try:
-                mw_instance = mw_class()
+                mw_instance = get_middleware_instance(middleware_path)
             except exceptions.MiddlewareNotUsed:
                 continue
 
                 request_middleware.append(mw_instance.process_request)
             if hasattr(mw_instance, 'process_view'):
                 self._view_middleware.append(mw_instance.process_view)
+            if hasattr(mw_instance, 'process_template_response'):
+                self._template_response_middleware.insert(0, mw_instance.process_template_response)
             if hasattr(mw_instance, 'process_response'):
                 self._response_middleware.insert(0, mw_instance.process_response)
             if hasattr(mw_instance, 'process_exception'):
             urlresolvers.set_urlconf(None)
 
         try:
+            # Apply template response middleware and the bake the response
+            # if the response can be baked
+            if hasattr(response, 'bake') and callable(response.bake):
+                for middleware_method in self._template_response_middleware:
+                    response = middleware_method(request, response)
+                response.bake()
+
             # Apply response middleware, regardless of the response
             for middleware_method in self._response_middleware:
                 response = middleware_method(request, response)
         return force_unicode(script_url[:-len(environ.get('PATH_INFO', ''))])
     return force_unicode(environ.get('SCRIPT_NAME', u''))
 
+def get_middleware_instance(middleware_path):
+    """
+    Returns middleware instance
+    """
+    try:
+        dot = middleware_path.rindex('.')
+    except ValueError:
+        raise exceptions.ImproperlyConfigured('%s isn\'t a middleware module' % middleware_path)
+    mw_module, mw_classname = middleware_path[:dot], middleware_path[dot+1:]
+    try:
+        mod = import_module(mw_module)
+    except ImportError, e:
+        raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
+    try:
+        mw_class = getattr(mod, mw_classname)
+    except AttributeError:
+        raise exceptions.ImproperlyConfigured('Middleware module "%s" does not define a "%s" class' % (mw_module, mw_classname))
+
+    return mw_class()
+

File django/shortcuts/__init__.py

View file
  • Ignore whitespace
 """
 
 from django.template import loader
+from django.template.response import TemplateResponse
 from django.http import HttpResponse, Http404
 from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect
 from django.db.models.manager import Manager
     """
     Returns an HttpResponseRedirect to the apropriate URL for the arguments
     passed.
-    
+
     The arguments could be:
-    
+
         * A model: the model's `get_absolute_url()` function will be called.
-    
+
         * A view name, possibly with arguments: `urlresolvers.reverse()` will
           be used to reverse-resolve the name.
-         
+
         * A URL, which will be used as-is for the redirect location.
-        
+
     By default issues a temporary redirect; pass permanent=True to issue a
     permanent redirect
     """
         redirect_class = HttpResponsePermanentRedirect
     else:
         redirect_class = HttpResponseRedirect
-    
+
     # If it's a model, use get_absolute_url()
     if hasattr(to, 'get_absolute_url'):
         return redirect_class(to.get_absolute_url())
-    
+
     # Next try a reverse URL resolution.
     try:
         return redirect_class(urlresolvers.reverse(to, args=args, kwargs=kwargs))
         # If this doesn't "feel" like a URL, re-raise.
         if '/' not in to and '.' not in to:
             raise
-        
+
     # Finally, fall back and assume it's a URL
     return redirect_class(to)
 
     obj_list = list(queryset.filter(*args, **kwargs))
     if not obj_list:
         raise Http404('No %s matches the given query.' % queryset.model._meta.object_name)
-    return obj_list
+    return obj_list
+
+render = TemplateResponse

File django/template/response.py

View file
  • Ignore whitespace
+from django.http import HttpResponse
+from django.template import loader, Context, RequestContext
+
+class ContentNotReadyError(Exception):
+    pass
+
+class SimpleTemplateResponse(HttpResponse):
+
+    def __init__(self, template, context=None, mimetype=None, status=None,
+            content_type=None):
+        # These two properties were originally called 'template' and 'context'
+        # but django.test.client.Client was clobbering those leading to really
+        # tricky-to-debug problems
+        self.template_name = template
+        self.template_context = context
+        self.baked = False
+
+        # content argument doesn't make sense here because it will be replaced
+        # with rendered template so we always pass empty string in order to
+        # prevent errors and provide shorter signature.
+        super(SimpleTemplateResponse, self).__init__('', mimetype, status,
+                                                     content_type)
+
+    def resolve_template(self, template):
+        "Accepts a template object, path-to-template or list of paths"
+        if isinstance(template, (list, tuple)):
+            return loader.select_template(template)
+        elif isinstance(template, basestring):
+            return loader.get_template(template)
+        else:
+            return template
+
+    def resolve_context(self, context):
+        "context can be a dictionary or a context object"
+        if isinstance(context, Context):
+            return context
+        else:
+            return Context(context)
+
+    def render(self):
+        template = self.resolve_template(self.template_name)
+        context = self.resolve_context(self.template_context)
+        content = template.render(context)
+        return content
+
+    def bake(self):
+        self._set_content(self.render())
+        return self
+
+    def __iter__(self):
+        if not self.baked:
+            raise ContentNotReadyError('The response content is not ready. '
+                                       'It must be baked before the iteration.')
+        return super(SimpleTemplateResponse, self).__iter__()
+
+    def _get_content(self):
+        if not self.baked:
+            raise ContentNotReadyError('The response content is not ready. '
+                                       'It must be baked before the first access.')
+        return super(SimpleTemplateResponse, self)._get_content()
+
+    def _set_content(self, value):
+        "Overrides rendered content, unless you later call bake()"
+        super(SimpleTemplateResponse, self)._set_content(value)
+        self.baked = True
+
+    content = property(_get_content, _set_content)
+
+class TemplateResponse(SimpleTemplateResponse):
+
+    def __init__(self, request, template, context=None, mimetype=None,
+            status=None, content_type=None):
+        # self.request gets over-written by django.test.client.Client - and
+        # unlike template_context and template_name the _request should not
+        # be considered part of the public API.
+        self._request = request
+        super(TemplateResponse, self).__init__(
+            template, context, mimetype, status, content_type)
+
+    def resolve_context(self, context):
+        if isinstance(context, Context):
+            return context
+        else:
+            return RequestContext(self._request, context)
+
+def bake(response):
+    """
+    Bakes response if it can be baked. HttpResponse instances are unaffected.
+    """
+    if (hasattr(response, 'bake') and callable(response.bake)):
+        response.bake()

File django/test/client.py

View file
  • Ignore whitespace
 from django.core.signals import got_request_exception
 from django.http import SimpleCookie, HttpRequest, QueryDict
 from django.template import TemplateDoesNotExist
+from django.template.response import bake
 from django.test import signals
 from django.utils.functional import curry
 from django.utils.encoding import smart_str
 
             try:
                 response = self.handler(environ)
+                bake(response)
+
             except TemplateDoesNotExist, e:
                 # If the view raises an exception, Django will attempt to show
                 # the 500.html template. If that template is not available,

File django/test/testcases.py

View file
  • Ignore whitespace
 from django.http import QueryDict
 from django.test import _doctest as doctest
 from django.test.client import Client
+from django.template.response import bake
 from django.utils import simplejson, unittest as ut2
 from django.utils.encoding import smart_str
 from django.utils.functional import wraps
         if msg_prefix:
             msg_prefix += ": "
 
+        bake(response)
+
         # Put context(s) into a list to simplify processing.
         contexts = to_list(response.context)
         if not contexts:
         Asserts that the template with the provided name was used in rendering
         the response.
         """
+
         if msg_prefix:
             msg_prefix += ": "
 
+        bake(response)
+
         template_names = [t.name for t in response.templates]
         if not template_names:
             self.fail(msg_prefix + "No templates used to render the response")
         if msg_prefix:
             msg_prefix += ": "
 
+        bake(response)
+
         template_names = [t.name for t in response.templates]
         self.failIf(template_name in template_names,
             msg_prefix + "Template '%s' was used unexpectedly in rendering"

File django/views/generic/base.py

View file
  • Ignore whitespace
 from django import http
 from django.core.exceptions import ImproperlyConfigured
 from django.template import RequestContext, loader
+from django.template.response import TemplateResponse
 from django.utils.translation import ugettext_lazy as _
 from django.utils.functional import update_wrapper
 from django.utils.log import getLogger
     A mixin that can be used to render a template.
     """
     template_name = None
+    template_response_class = TemplateResponse
 
-    def render_to_response(self, context):
+    def render_to_response(self, context, **response_kwargs):
         """
         Returns a response with a template rendered with the given context.
         """
-        return self.get_response(self.render_template(context))
-
-    def get_response(self, content, **httpresponse_kwargs):
-        """
-        Construct an `HttpResponse` object.
-        """
-        return http.HttpResponse(content, **httpresponse_kwargs)
-
-    def render_template(self, context):
-        """
-        Render the template with a given context.
-        """
-        context_instance = self.get_context_instance(context)
-        return self.get_template().render(context_instance)
-
-    def get_context_instance(self, context):
-        """
-        Get the template context instance. Must return a Context (or subclass)
-        instance.
-        """
-        return RequestContext(self.request, context)
-
-    def get_template(self):
-        """
-        Get a ``Template`` object for the given request.
-        """
-        names = self.get_template_names()
-        if not names:
-            raise ImproperlyConfigured(u"'%s' must provide template_name."
-                                       % self.__class__.__name__)
-        return self.load_template(names)
+        return self.template_response_class(
+            request = self.request,
+            template =  self.get_template_names(),
+            context = context,
+            **response_kwargs
+        )
 
     def get_template_names(self):
         """
-        Return a list of template names to be used for the request. Must return
-        a list. May not be called if get_template is overridden.
+        Returns a list of template names to be used for the request. Must return
+        a list. May not be called if render_to_response is overridden.
         """
         if self.template_name is None:
             return []
         else:
             return [self.template_name]
 
-    def load_template(self, names):
-        """
-        Load a list of templates using the default template loader.
-        """
-        return loader.select_template(names)
-
 
 class TemplateView(TemplateResponseMixin, View):
     """

File docs/index.txt

View file
  • Ignore whitespace
       :doc:`View functions <topics/http/views>` |
       :doc:`Shortcuts <topics/http/shortcuts>`
 
-    * **Reference:**  :doc:`Request/response objects <ref/request-response>`
+    * **Reference:**
+      :doc:`Request/response objects <ref/request-response>` |
+      :doc:`TemplateResponse <ref/template-response>`
 
     * **File uploads:**
       :doc:`Overview <topics/http/file-uploads>` |

File docs/ref/class-based-views.txt

View file
  • Ignore whitespace
 
         The path to the template to use when rendering the view.
 
-    .. method:: render_to_response(context)
+    .. attribute:: template_response_class
 
-        Returns a full composed HttpResponse instance, ready to be returned to
-        the user.
+        The response class to be returned by ``render_to_response`` method.
+        Default is
+        :class:`TemplateResponse <django.template.response.TemplateResponse>`.
+        The template and context of TemplateResponse instances can be
+        altered later (e.g. in
+        :ref:`template response middleware <template-response-middleware>`).
 
-        Calls :meth:`~TemplateResponseMixin.render_template()` to build the
-        content of the response, and
-        :meth:`~TemplateResponseMixin.get_response()` to construct the
-        :class:`~django.http.HttpResponse` object.
+        Create TemplateResponse subclass and pass set it to
+        ``template_response_class`` if you need custom template loading or
+        custom context object instantiation.
 
-    .. method:: get_response(content, **httpresponse_kwargs)
+    .. method:: render_to_response(context, **response_kwargs)
 
-        Constructs the :class:`~django.http.HttpResponse` object around the
-        given content. If any keyword arguments are provided, they will be
-        passed to the constructor of the :class:`~django.http.HttpResponse`
-        instance.
+        Returns a ``self.template_response_class`` instance.
 
-    .. method:: render_template(context)
-
-        Calls :meth:`~TemplateResponseMixin.get_context_instance()` to obtain
-        the :class:`Context` instance to use for rendering, and calls
-        :meth:`TemplateReponseMixin.get_template()` to load the template that
-        will be used to render the final content.
-
-    .. method:: get_context_instance(context)
-
-        Turns the data dictionary ``context`` into an actual context instance
-        that can be used for rendering.
-
-        By default, constructs a :class:`~django.template.RequestContext`
-        instance.
-
-    .. method:: get_template()
+        If any keyword arguments are provided, they will be
+        passed to the constructor of the response instance.
 
         Calls :meth:`~TemplateResponseMixin.get_template_names()` to obtain the
         list of template names that will be searched looking for an existent
         default implementation will return a list containing
         :attr:`TemplateResponseMixin.template_name` (if it is specified).
 
-    .. method:: load_template(names)
-
-        Loads and returns a template found by searching the list of ``names``
-        for a match. Uses Django's default template loader.
 
 Single object mixins
 --------------------

File docs/ref/index.txt

View file
  • Ignore whitespace
    middleware
    models/index
    request-response
+   template-response
    settings
    signals
    templates/index

File docs/ref/request-response.txt

View file
  • Ignore whitespace
 .. class:: HttpResponseServerError
 
     Acts just like :class:`HttpResponse` but uses a 500 status code.
+
+TemplateResponse
+----------------
+
+.. versionadded:: 1.3
+
+See :doc:`TemplateResponse <ref/template-response>` for more.

File docs/ref/template-response.txt

View file
  • Ignore whitespace
+===========================================
+TemplateResponse and SimpleTemplateResponse
+===========================================
+
+.. versionadded:: 1.3
+
+.. module:: django.template.response
+   :synopsis: Classes dealing with lazy-rendered HTTP responses.
+
+Quick overview
+==============
+
+An HttpResponse with an opaque string as its content is hard to alter after
+it is returned from a view.
+
+Special HttpResponse subclass - TemplateResponse is introduced in Django to
+solve this problem. It can keep a template and its context separate until
+the very last moment when response middleware is applied and only then do
+render a template. Such lazy template rendering is called 'baking' throught
+the docs.
+
+This way it is possible to add something to the view's context before it
+blends in a template or to change the template that will be rendered.
+
+This document explains the APIs for :class:`SimpleTemplateResponse` and
+:class:`TemplateResponse` objects.
+
+SimpleTemplateResponse objects
+==============================
+
+.. class:: SimpleTemplateResponse()
+
+Attributes
+----------
+
+.. attribute:: SimpleTemplateResponse.template_name
+
+    Template to be rendered. Accepts :class:`django.template.Template` object,
+    path to template or list of paths.
+
+    Example: ``['foo.html', 'bar.html']``
+
+.. attribute:: SimpleTemplateResponse.template_context
+
+    Context to be used with template. It can be a dictionary or a context
+    object.
+
+    Example: ``{'foo': 123}``
+
+
+Methods
+-------
+
+.. method:: SimpleTemplateResponse.__init__(template, context=None, mimetype=None, status=None, content_type=None)
+
+   Instantiates an ``SimpleTemplateResponse`` object with the given template,
+   context, MIME type and HTTP status.
+
+   ``template`` is a full name of a template to use or sequence of
+   template names. :class:`django.template.Template` instances are also
+   accepted.
+
+   ``context`` is a dictionary of values to add to the template context.
+   By default, this is an empty dictionary; context objects are also
+   accepted as ``context`` values.
+
+   ``status`` is the HTTP Status code for the response.
+
+   ``content_type`` is an alias for ``mimetype``. Historically, this parameter
+   was only called ``mimetype``, but since this is actually the value included
+   in the HTTP ``Content-Type`` header, it can also include the character set
+   encoding, which makes it more than just a MIME type specification.
+   If ``mimetype`` is specified (not ``None``), that value is used.
+   Otherwise, ``content_type`` is used. If neither is given, the
+   ``DEFAULT_CONTENT_TYPE`` setting is used.
+
+
+.. method:: SimpleTemplateResponse.resolve_context(context)
+
+   Accepts dictionary or a context object.
+   Returns :class:`django.template.Context` instance.
+
+   Override this method in order to customize context instantiation.
+
+
+.. method:: SimpleTemplateResponse.resolve_template(template)
+
+   Accepts a full name of a template to use or sequence of template names.
+   :class:`django.template.Template` instances are also accepted.
+   Returns :class:`django.template.Template` instance to be rendered.
+
+   Override this method in order to customize template rendering.
+
+.. method:: SimpleTemplateResponse.render():
+
+   Renders a template instance returned by
+   :meth:`SimpleTemplateResponse.resolve_template` using the context instance
+   returned by :meth:`SimpleTemplateResponse.resolve_context` and returns the
+   rendered content.
+
+.. method:: SimpleTemplateResponse.bake():
+
+   Bakes the response by setting :attr:`response.content` to the result
+   returned by :meth:`SimpleTemplateResponse.render`.
+
+   The response is baked by Django during response middleware cycle. You should
+   only bake the response yourself if it is to be used outside the
+   request-response cycle (e.g. for debugging). It will be baked twice
+   otherwise.
+
+
+
+TemplateResponse objects
+========================
+
+.. class:: TemplateResponse()
+
+   TemplateResponse is a subclass of
+   :class:`SimpleTemplateResponse <django.template.response.SimpleTemplateResponse>`
+   that uses RequestContext instead of Context.
+
+.. method:: TemplateResponse.__init__(request, template, context=None, mimetype=None, status=None, content_type=None)
+
+   Instantiates an ``TemplateResponse`` object with the given template,
+   context, MIME type and HTTP status.
+
+   ``request`` is a HttpRequest instance.
+
+   ``template`` is a full name of a template to use or sequence of
+   template names. :class:`django.template.Template` instances are also
+   accepted.
+
+   ``context`` is a dictionary of values to add to the template context.
+   By default, this is an empty dictionary; context objects are also accepted
+   as ``context`` values.
+
+   ``status`` is the HTTP Status code for the response.
+
+   ``content_type`` is an alias for ``mimetype``. Historically, this parameter
+   was only called ``mimetype``, but since this is actually the value included
+   in the HTTP ``Content-Type`` header, it can also include the character set
+   encoding, which makes it more than just a MIME type specification.
+   If ``mimetype`` is specified (not ``None``), that value is used.
+   Otherwise, ``content_type`` is used. If neither is given, the
+   ``DEFAULT_CONTENT_TYPE`` setting is used.
+

File docs/topics/http/middleware.txt

View file
  • Ignore whitespace
 classes are applied in reverse order, from the bottom up. This means classes
 defined at the end of :setting:`MIDDLEWARE_CLASSES` will be run first.
 
+.. _template-response-middleware:
+
+``process_template_response``
+-----------------------------
+
+.. versionadded:: 1.3
+
+.. method:: process_template_response(self, request, response)
+
+``request`` is an :class:`~django.http.HttpRequest` object. ``response`` is the
+:class:`~django.template.response.SimpleTemplateResponse` subclass (e.g.
+:class:`~django.template.response.TemplateResponse`) object returned by a
+Django view.
+
+``process_template_response()`` must return an
+:class:`~django.template.response.SimpleTemplateResponse` (or it's subclass)
+object. It could alter the given ``response`` by changing
+``response.template_name`` and ``response.template_context``, or it could
+create and return a brand-new
+:class:`~django.template.response.SimpleTemplateResponse` (or it's subclass)
+instance.
+
+``process_template_response()`` method is called only on response instances that
+can be baked. It is called before the ``process_response()`` method.
+
+You shouldn't bake the response in ``process_template_response``. It
+will be baked just before applying response middlewares. This way template
+will be rendered exactly 1 time.
+
+Middleware are run in reverse order during the response phase, which
+includes process_template_response.
+
 .. _exception-middleware:
 
 ``process_exception``

File docs/topics/http/shortcuts.txt

View file
  • Ignore whitespace
 "span" multiple levels of MVC. In other words, these functions/classes
 introduce controlled coupling for convenience's sake.
 
+``render``
+==========
+
+.. function:: render(request, template[, context][, mimetype][, status][, content_type])
+
+   Combines a given template with a given context dictionary and returns an
+   :class:`~django.template.response.TemplateResponse` object.
+
+   TODO
+
+Examples
+--------
+
+TODO
+
+
+
 ``render_to_response``
 ======================
 

File tests/regressiontests/generic_views/base.py

View file
  • Ignore whitespace
     rf = RequestFactory()
 
     def _assert_about(self, response):
+        response.bake()
         self.assertEqual(response.status_code, 200)
         self.assertEqual(response.content, '<h1>About</h1>')
 

File tests/regressiontests/templates/response.py

View file
  • Ignore whitespace
+import os
+from django.utils import unittest
+from django.test import RequestFactory
+from django.conf import settings
+import django.template.context
+from django.template import Template, Context, RequestContext
+from django.template.response import (TemplateResponse, SimpleTemplateResponse,
+                                      ContentNotReadyError)
+
+def test_processor(request):
+    return {'processors': 'yes'}
+test_processor_name = 'regressiontests.templates.response.test_processor'
+
+class BaseTemplateResponseTest(unittest.TestCase):
+    # tests rely on fact that global context
+    # processors should only work when RequestContext is used.
+
+    def setUp(self):
+        self.factory = RequestFactory()
+        self._old_processors = settings.TEMPLATE_CONTEXT_PROCESSORS
+        self._old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS
+        settings.TEMPLATE_CONTEXT_PROCESSORS = [test_processor_name]
+        settings.TEMPLATE_DIRS = (
+            os.path.join(
+                os.path.dirname(__file__),
+                'templates'
+            ),
+        )
+        # Force re-evaluation of the contex processor list
+        django.template.context._standard_context_processors = None
+
+    def tearDown(self):
+        settings.TEMPLATE_DIRS = self._old_TEMPLATE_DIRS
+        settings.TEMPLATE_CONTEXT_PROCESSORS = self._old_processors
+        # Force re-evaluation of the contex processor list
+        django.template.context._standard_context_processors = None
+
+
+class SimpleTemplateResponseTest(BaseTemplateResponseTest):
+
+    def _response(self, template='foo', *args, **kwargs):
+        return SimpleTemplateResponse(Template(template), *args, **kwargs)
+
+    def test_template_resolving(self):
+        response = SimpleTemplateResponse('first/test.html')
+        response.bake()
+        self.assertEqual('First template\n', response.content)
+
+        templates = ['foo.html', 'second/test.html', 'first/test.html']
+        response = SimpleTemplateResponse(templates)
+        response.bake()
+        self.assertEqual('Second template\n', response.content)
+
+        response = self._response()
+        response.bake()
+        self.assertEqual(response.content, 'foo')
+
+    def test_explicit_baking(self):
+        # explicit baking
+        response = self._response()
+        self.assertFalse(response.baked)
+        response.bake()
+        self.assertTrue(response.baked)
+
+    def test_bake(self):
+        # response is not re-baked without the bake call
+        response = self._response().bake()
+        self.assertEqual(response.content, 'foo')
+
+        response.template_name = Template('bar{{ baz }}')
+        self.assertEqual(response.content, 'foo')
+        response.bake()
+        self.assertEqual(response.content, 'bar')
+
+        response.template_context = {'baz': 'baz'}
+        self.assertEqual(response.content, 'bar')
+        response.bake()
+        self.assertEqual(response.content, 'barbaz')
+
+        response.template_context = {'baz': 'spam'}
+        for x in response:
+            pass
+        self.assertEqual(response.content, 'barbaz')
+
+    def test_iteration_unbaked(self):
+        # unbaked response raises an exception on iteration
+        response = self._response()
+        self.assertFalse(response.baked)
+
+        def iteration():
+            for x in response:
+                pass
+        self.assertRaises(ContentNotReadyError, iteration)
+        self.assertFalse(response.baked)
+
+    def test_iteration_baked(self):
+        # iteration works for baked responses
+        response = self._response().bake()
+        res = [x for x in response]
+        self.assertEqual(res, ['foo'])
+
+    def test_content_access_unbaked(self):
+        # unbaked response raises an exception when content is accessed
+        response = self._response()
+        self.assertFalse(response.baked)
+        self.assertRaises(ContentNotReadyError, lambda: response.content)
+        self.assertFalse(response.baked)
+
+    def test_content_access_baked(self):
+        # baked response content can be accessed
+        response = self._response().bake()
+        self.assertEqual(response.content, 'foo')
+
+    def test_set_content(self):
+        # content can be overriden
+        response = self._response()
+        self.assertFalse(response.baked)
+        response.content = 'spam'
+        self.assertTrue(response.baked)
+        self.assertEqual(response.content, 'spam')
+        response.content = 'baz'
+        self.assertEqual(response.content, 'baz')
+
+    def test_dict_context(self):
+        response = self._response('{{ foo }}{{ processors }}',
+                                  {'foo': 'bar'})
+        self.assertEqual(response.template_context, {'foo': 'bar'})
+        response.bake()
+        self.assertEqual(response.content, 'bar')
+
+    def test_context_instance(self):
+        response = self._response('{{ foo }}{{ processors }}',
+                                  Context({'foo': 'bar'}))
+        self.assertEqual(response.template_context.__class__, Context)
+        response.bake()
+        self.assertEqual(response.content, 'bar')
+
+    def test_kwargs(self):
+        response = self._response(content_type = 'application/json', status=504)
+        self.assertEqual(response['content-type'], 'application/json')
+        self.assertEqual(response.status_code, 504)
+
+    def test_args(self):
+        response = SimpleTemplateResponse('', {}, 'application/json', 504)
+        self.assertEqual(response['content-type'], 'application/json')
+        self.assertEqual(response.status_code, 504)
+
+
+class TemplateResponseTest(BaseTemplateResponseTest):
+
+    def _response(self, template='foo', *args, **kwargs):
+        return TemplateResponse(self.factory.get('/'), Template(template),
+                                *args, **kwargs)
+
+    def test_render(self):
+        response = self._response('{{ foo }}{{ processors }}').bake()
+        self.assertEqual(response.content, 'yes')
+
+    def test_render_with_requestcontext(self):
+        response = self._response('{{ foo }}{{ processors }}',
+                                  {'foo': 'bar'}).bake()
+        self.assertEqual(response.content, 'baryes')
+
+    def test_render_with_context(self):
+        response = self._response('{{ foo }}{{ processors }}',
+                                  Context({'foo': 'bar'})).bake()
+        self.assertEqual(response.content, 'bar')
+
+    def test_kwargs(self):
+        response = self._response(content_type = 'application/json',
+                                  status=504)
+        self.assertEqual(response['content-type'], 'application/json')
+        self.assertEqual(response.status_code, 504)
+
+    def test_args(self):
+        response = TemplateResponse(self.factory.get('/'), '', {},
+                                    'application/json', 504)
+        self.assertEqual(response['content-type'], 'application/json')
+        self.assertEqual(response.status_code, 504)

File tests/regressiontests/templates/tests.py

View file
  • Ignore whitespace
 from unicode import unicode_tests
 from nodelist import NodelistTest
 from smartif import *
+from response import *
 
 try:
     from loaders import *

File tests/regressiontests/views/templates/debug/render_test.html

View file
  • Ignore whitespace
+{{ foo }}.{{ bar }}.{{ baz }}.{{ processors }}

File tests/regressiontests/views/tests/__init__.py

View file
  • Ignore whitespace
 from i18n import *
 from specials import *
 from static import *
+from shortcuts import *

File tests/regressiontests/views/tests/shortcuts.py

View file
  • Ignore whitespace
+from django.test import RequestFactory
+from django.template import Template
+from django.shortcuts import render
+
+from regressiontests.templates.response import BaseTemplateResponseTest
+
+def library_view(request):
+    return render(request, 'debug/render_test.html',
+                  {'foo': 'foo', 'baz': 'baz'})
+
+def customized_context_view(request):
+    response = library_view(request)
+    response.template_context.update({'bar': 'bar', 'baz': 'spam'})
+    return response
+
+def customized_template_view(request):
+    response = library_view(request)
+    response.template_name = Template('context does not matter')
+    return response
+
+
+class RenderTest(BaseTemplateResponseTest):
+
+    def setUp(self):
+        super(RenderTest, self).setUp()
+        self.request = self.factory.get('/')
+
+    def test_library_view(self):
+        response = library_view(self.request).bake()
+        self.assertEqual(response.content, 'foo..baz.yes\n')
+
+    def test_customized_context(self):
+        response = customized_context_view(self.request).bake()
+        self.assertEqual(response.content, 'foo.bar.spam.yes\n')
+
+    def test_customized_template(self):
+        response = customized_template_view(self.request).bake()
+        self.assertEqual(response.content, 'context does not matter')
+
+    def test_status_and_content_type(self):
+        response = render(self.request, 'base.html', {'foo': 'bar'},
+                          'application/json', 504)
+        self.assertEqual(response.status_code, 504)
+        self.assertEqual(response['content-type'], 'application/json')
+