diff -r 8343c0d86151 django/contrib/messages/middleware.py
--- a/django/contrib/messages/middleware.py	Tue Nov 02 13:42:30 2010 +0000
+++ b/django/contrib/messages/middleware.py	Tue Nov 02 19:57:53 2010 +0500
@@ -1,7 +1,6 @@
 from django.conf import settings
 from django.contrib.messages.storage import default_storage
 
-
 class MessageMiddleware(object):
     """
     Middleware that handles temporary messages.
diff -r 8343c0d86151 django/contrib/messages/tests/base.py
--- a/django/contrib/messages/tests/base.py	Tue Nov 02 13:42:30 2010 +0000
+++ b/django/contrib/messages/tests/base.py	Tue Nov 02 19:57:53 2010 +0500
@@ -96,7 +96,7 @@
         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)
 
@@ -173,6 +173,27 @@
             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
diff -r 8343c0d86151 django/contrib/messages/tests/urls.py
--- a/django/contrib/messages/tests/urls.py	Tue Nov 02 13:42:30 2010 +0000
+++ b/django/contrib/messages/tests/urls.py	Tue Nov 02 19:57:53 2010 +0500
@@ -2,9 +2,20 @@
 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
@@ -16,24 +27,25 @@
                                             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),
 )
diff -r 8343c0d86151 django/core/handlers/base.py
--- a/django/core/handlers/base.py	Tue Nov 02 13:42:30 2010 +0000
+++ b/django/core/handlers/base.py	Tue Nov 02 19:57:53 2010 +0500
@@ -21,6 +21,7 @@
     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.
@@ -30,27 +31,14 @@
         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
 
@@ -58,6 +46,8 @@
                 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'):
@@ -162,6 +152,13 @@
             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)
@@ -240,3 +237,23 @@
         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()
+
diff -r 8343c0d86151 django/shortcuts/__init__.py
--- a/django/shortcuts/__init__.py	Tue Nov 02 13:42:30 2010 +0000
+++ b/django/shortcuts/__init__.py	Tue Nov 02 19:57:53 2010 +0500
@@ -5,6 +5,7 @@
 """
 
 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
@@ -23,16 +24,16 @@
     """
     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
     """
@@ -40,11 +41,11 @@
         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))
@@ -55,7 +56,7 @@
         # 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)
 
@@ -101,4 +102,6 @@
     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
\ No newline at end of file
+    return obj_list
+
+render = TemplateResponse
diff -r 8343c0d86151 django/template/response.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/django/template/response.py	Tue Nov 02 19:57:53 2010 +0500
@@ -0,0 +1,91 @@
+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()
diff -r 8343c0d86151 django/test/testcases.py
--- a/django/test/testcases.py	Tue Nov 02 13:42:30 2010 +0000
+++ b/django/test/testcases.py	Tue Nov 02 19:57:53 2010 +0500
@@ -484,6 +484,7 @@
         Asserts that the template with the provided name was used in rendering
         the response.
         """
+
         if msg_prefix:
             msg_prefix += ": "
 
diff -r 8343c0d86151 django/views/generic/base.py
--- a/django/views/generic/base.py	Tue Nov 02 13:42:30 2010 +0000
+++ b/django/views/generic/base.py	Tue Nov 02 19:57:53 2010 +0500
@@ -2,6 +2,7 @@
 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
@@ -88,59 +89,29 @@
     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):
     """
diff -r 8343c0d86151 docs/index.txt
--- a/docs/index.txt	Tue Nov 02 13:42:30 2010 +0000
+++ b/docs/index.txt	Tue Nov 02 19:57:53 2010 +0500
@@ -93,7 +93,9 @@
       :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>` |
diff -r 8343c0d86151 docs/ref/class-based-views.txt
--- a/docs/ref/class-based-views.txt	Tue Nov 02 13:42:30 2010 +0000
+++ b/docs/ref/class-based-views.txt	Tue Nov 02 19:57:53 2010 +0500
@@ -76,39 +76,25 @@
 
         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
@@ -123,10 +109,6 @@
         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
 --------------------
diff -r 8343c0d86151 docs/ref/index.txt
--- a/docs/ref/index.txt	Tue Nov 02 13:42:30 2010 +0000
+++ b/docs/ref/index.txt	Tue Nov 02 19:57:53 2010 +0500
@@ -16,6 +16,7 @@
    middleware
    models/index
    request-response
+   template-response
    settings
    signals
    templates/index
diff -r 8343c0d86151 docs/ref/request-response.txt
--- a/docs/ref/request-response.txt	Tue Nov 02 13:42:30 2010 +0000
+++ b/docs/ref/request-response.txt	Tue Nov 02 19:57:53 2010 +0500
@@ -645,3 +645,10 @@
 .. 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.
diff -r 8343c0d86151 docs/ref/template-response.txt
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/docs/ref/template-response.txt	Tue Nov 02 19:57:53 2010 +0500
@@ -0,0 +1,146 @@
+===========================================
+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.
+
diff -r 8343c0d86151 docs/topics/http/middleware.txt
--- a/docs/topics/http/middleware.txt	Tue Nov 02 13:42:30 2010 +0000
+++ b/docs/topics/http/middleware.txt	Tue Nov 02 19:57:53 2010 +0500
@@ -120,6 +120,38 @@
 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``
diff -r 8343c0d86151 docs/topics/http/shortcuts.txt
--- a/docs/topics/http/shortcuts.txt	Tue Nov 02 13:42:30 2010 +0000
+++ b/docs/topics/http/shortcuts.txt	Tue Nov 02 19:57:53 2010 +0500
@@ -12,6 +12,23 @@
 "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``
 ======================
 
diff -r 8343c0d86151 tests/regressiontests/generic_views/base.py
--- a/tests/regressiontests/generic_views/base.py	Tue Nov 02 13:42:30 2010 +0000
+++ b/tests/regressiontests/generic_views/base.py	Tue Nov 02 19:57:53 2010 +0500
@@ -156,6 +156,7 @@
     rf = RequestFactory()
 
     def _assert_about(self, response):
+        response.bake()
         self.assertEqual(response.status_code, 200)
         self.assertEqual(response.content, '<h1>About</h1>')
 
diff -r 8343c0d86151 tests/regressiontests/middleware_exceptions/tests.py
--- a/tests/regressiontests/middleware_exceptions/tests.py	Tue Nov 02 13:42:30 2010 +0000
+++ b/tests/regressiontests/middleware_exceptions/tests.py	Tue Nov 02 19:57:53 2010 +0500
@@ -3,7 +3,8 @@
 from django.test import TestCase
 from django.core.signals import got_request_exception
 from django.http import HttpResponse
-
+from django.template.response import TemplateResponse
+from django.template import Template
 
 class TestException(Exception):
     pass
@@ -15,6 +16,7 @@
         self.process_request_called = False
         self.process_view_called = False
         self.process_response_called = False
+        self.process_template_response_called = False
         self.process_exception_called = False
 
     def process_request(self, request):
@@ -23,6 +25,10 @@
     def process_view(self, request, view_func, view_args, view_kwargs):
         self.process_view_called = True
 
+    def process_template_response(self, request, response):
+        self.process_template_response_called = True
+        return response
+
     def process_response(self, request, response):
         self.process_response_called = True
         return response
@@ -47,6 +53,11 @@
         super(ResponseMiddleware, self).process_response(request, response)
         return HttpResponse('Response Middleware')
 
+class TemplateResponseMiddleware(TestMiddleware):
+    def process_template_response(self, request, response):
+        super(TemplateResponseMiddleware, self).process_template_response(request, response)
+        return TemplateResponse(request, Template('Template Response Middleware'))
+
 class ExceptionMiddleware(TestMiddleware):
     def process_exception(self, request, exception):
         super(ExceptionMiddleware, self).process_exception(request, exception)
@@ -65,6 +76,11 @@
         super(BadViewMiddleware, self).process_view(request, view_func, view_args, view_kwargs)
         raise TestException('Test View Exception')
 
+class BadTemplateResponseMiddleware(TestMiddleware):
+    def process_template_response(self, request, response):
+        super(BadTemplateResponseMiddleware, self).process_template_response(request, response)
+        raise TestException('Test Template Response Exception')
+
 class BadResponseMiddleware(TestMiddleware):
     def process_response(self, request, response):
         super(BadResponseMiddleware, self).process_response(request, response)
@@ -92,6 +108,7 @@
     def _add_middleware(self, middleware):
         self.client.handler._request_middleware.insert(0, middleware.process_request)
         self.client.handler._view_middleware.insert(0, middleware.process_view)
+        self.client.handler._template_response_middleware.append(middleware.process_template_response)
         self.client.handler._response_middleware.append(middleware.process_response)
         self.client.handler._exception_middleware.append(middleware.process_exception)
 
@@ -112,9 +129,10 @@
             exception, value, tb = self.exceptions[i]
             self.assertEquals(value.args, (error, ))
 
-    def assert_middleware_usage(self, middleware, request, view, response, exception):
+    def assert_middleware_usage(self, middleware, request, view, template_response, response, exception):
         self.assertEqual(middleware.process_request_called, request)
         self.assertEqual(middleware.process_view_called, view)
+        self.assertEqual(middleware.process_template_response_called, template_response)
         self.assertEqual(middleware.process_response_called, response)
         self.assertEqual(middleware.process_exception_called, exception)
 
@@ -131,9 +149,9 @@
         self.assert_exceptions_handled('/middleware_exceptions/view/', [])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  False, True,  False)
-        self.assert_middleware_usage(middleware,      True,  False, True,  False)
-        self.assert_middleware_usage(post_middleware, False, False, True,  False)
+        self.assert_middleware_usage(pre_middleware,  True,  False, False, True,  False)
+        self.assert_middleware_usage(middleware,      True,  False, False, True,  False)
+        self.assert_middleware_usage(post_middleware, False, False, False, True,  False)
 
     def test_process_view_middleware(self):
         pre_middleware = TestMiddleware()
@@ -145,9 +163,9 @@
         self.assert_exceptions_handled('/middleware_exceptions/view/', [])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
-        self.assert_middleware_usage(middleware,      True,  True,  True, False)
-        self.assert_middleware_usage(post_middleware, True,  False, True, False)
+        self.assert_middleware_usage(pre_middleware,  True,  True, False,  True, False)
+        self.assert_middleware_usage(middleware,      True,  True, False,  True, False)
+        self.assert_middleware_usage(post_middleware, True,  False, False, True, False)
 
     def test_process_response_middleware(self):
         pre_middleware = TestMiddleware()
@@ -159,9 +177,23 @@
         self.assert_exceptions_handled('/middleware_exceptions/view/', [])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  True,  True,  False)
-        self.assert_middleware_usage(middleware,      True,  True,  True,  False)
-        self.assert_middleware_usage(post_middleware, True,  True,  True,  False)
+        self.assert_middleware_usage(pre_middleware,  True,  True, False, True,  False)
+        self.assert_middleware_usage(middleware,      True,  True, False, True,  False)
+        self.assert_middleware_usage(post_middleware, True,  True, False, True,  False)
+
+    def test_process_template_response_middleware(self):
+        pre_middleware = TestMiddleware()
+        middleware = TemplateResponseMiddleware()
+        post_middleware = TestMiddleware()
+        self._add_middleware(post_middleware)
+        self._add_middleware(middleware)
+        self._add_middleware(pre_middleware)
+        self.assert_exceptions_handled('/middleware_exceptions/template_response/', [])
+
+        # Check that the right middleware methods have been invoked
+        self.assert_middleware_usage(pre_middleware,  True,  True, True, True,  False)
+        self.assert_middleware_usage(middleware,      True,  True, True, True,  False)
+        self.assert_middleware_usage(post_middleware, True,  True, True, True,  False)
 
     def test_process_exception_middleware(self):
         pre_middleware = TestMiddleware()
@@ -173,9 +205,9 @@
         self.assert_exceptions_handled('/middleware_exceptions/view/', [])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
-        self.assert_middleware_usage(middleware,      True,  True,  True, False)
-        self.assert_middleware_usage(post_middleware, True,  True,  True, False)
+        self.assert_middleware_usage(pre_middleware,  True,  True, False, True, False)
+        self.assert_middleware_usage(middleware,      True,  True, False, True, False)
+        self.assert_middleware_usage(post_middleware, True,  True, False, True, False)
 
     def test_process_request_middleware_not_found(self):
         pre_middleware = TestMiddleware()
@@ -187,9 +219,9 @@
         self.assert_exceptions_handled('/middleware_exceptions/not_found/', [])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  False, True,  False)
-        self.assert_middleware_usage(middleware,      True,  False, True,  False)
-        self.assert_middleware_usage(post_middleware, False, False, True,  False)
+        self.assert_middleware_usage(pre_middleware,  True,  False, False, True,  False)
+        self.assert_middleware_usage(middleware,      True,  False, False, True,  False)
+        self.assert_middleware_usage(post_middleware, False, False, False, True,  False)
 
     def test_process_view_middleware_not_found(self):
         pre_middleware = TestMiddleware()
@@ -201,9 +233,23 @@
         self.assert_exceptions_handled('/middleware_exceptions/not_found/', [])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
-        self.assert_middleware_usage(middleware,      True,  True,  True, False)
-        self.assert_middleware_usage(post_middleware, True,  False, True, False)
+        self.assert_middleware_usage(pre_middleware,  True,  True, False,  True, False)
+        self.assert_middleware_usage(middleware,      True,  True, False,  True, False)
+        self.assert_middleware_usage(post_middleware, True,  False, False, True, False)
+
+    def test_process_template_response_middleware_not_found(self):
+        pre_middleware = TestMiddleware()
+        middleware = TemplateResponseMiddleware()
+        post_middleware = TestMiddleware()
+        self._add_middleware(post_middleware)
+        self._add_middleware(middleware)
+        self._add_middleware(pre_middleware)
+        self.assert_exceptions_handled('/middleware_exceptions/not_found/', [])
+
+        # Check that the right middleware methods have been invoked
+        self.assert_middleware_usage(pre_middleware,  True,  True, False, True,  True)
+        self.assert_middleware_usage(middleware,      True,  True, False, True,  True)
+        self.assert_middleware_usage(post_middleware, True,  True, False, True,  True)
 
     def test_process_response_middleware_not_found(self):
         pre_middleware = TestMiddleware()
@@ -215,9 +261,9 @@
         self.assert_exceptions_handled('/middleware_exceptions/not_found/', [])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  True,  True,  True)
-        self.assert_middleware_usage(middleware,      True,  True,  True,  True)
-        self.assert_middleware_usage(post_middleware, True,  True,  True,  True)
+        self.assert_middleware_usage(pre_middleware,  True,  True, False, True,  True)
+        self.assert_middleware_usage(middleware,      True,  True, False, True,  True)
+        self.assert_middleware_usage(post_middleware, True,  True, False, True,  True)
 
     def test_process_exception_middleware_not_found(self):
         pre_middleware = TestMiddleware()
@@ -229,9 +275,9 @@
         self.assert_exceptions_handled('/middleware_exceptions/not_found/', [])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
-        self.assert_middleware_usage(middleware,      True,  True,  True,  True)
-        self.assert_middleware_usage(post_middleware, True,  True,  True,  True)
+        self.assert_middleware_usage(pre_middleware,  True, True, False, True, False)
+        self.assert_middleware_usage(middleware,      True, True, False, True, True)
+        self.assert_middleware_usage(post_middleware, True, True, False, True, True)
 
     def test_process_request_middleware_exception(self):
         pre_middleware = TestMiddleware()
@@ -243,9 +289,9 @@
         self.assert_exceptions_handled('/middleware_exceptions/error/', [])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  False, True,  False)
-        self.assert_middleware_usage(middleware,  True,  False, True,  False)
-        self.assert_middleware_usage(post_middleware, False, False, True,  False)
+        self.assert_middleware_usage(pre_middleware, True, False, False, True,  False)
+        self.assert_middleware_usage(middleware,     True, False, False, True,  False)
+        self.assert_middleware_usage(post_middleware, False, False, False, True,  False)
 
     def test_process_view_middleware_exception(self):
         pre_middleware = TestMiddleware()
@@ -257,9 +303,9 @@
         self.assert_exceptions_handled('/middleware_exceptions/error/', [])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
-        self.assert_middleware_usage(middleware,      True,  True,  True, False)
-        self.assert_middleware_usage(post_middleware, True,  False, True, False)
+        self.assert_middleware_usage(pre_middleware,  True,  True, False, True, False)
+        self.assert_middleware_usage(middleware,      True,  True, False, True, False)
+        self.assert_middleware_usage(post_middleware, True,  False, False, True, False)
 
     def test_process_response_middleware_exception(self):
         pre_middleware = TestMiddleware()
@@ -271,9 +317,9 @@
         self.assert_exceptions_handled('/middleware_exceptions/error/', ['Error in view'], Exception())
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  True,  True,  True)
-        self.assert_middleware_usage(middleware,      True,  True,  True,  True)
-        self.assert_middleware_usage(post_middleware, True,  True,  True,  True)
+        self.assert_middleware_usage(pre_middleware,  True,  True, False, True,  True)
+        self.assert_middleware_usage(middleware,      True,  True, False, True,  True)
+        self.assert_middleware_usage(post_middleware, True,  True, False, True,  True)
 
     def test_process_exception_middleware_exception(self):
         pre_middleware = TestMiddleware()
@@ -285,9 +331,9 @@
         self.assert_exceptions_handled('/middleware_exceptions/error/', [])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
-        self.assert_middleware_usage(middleware,      True,  True,  True,  True)
-        self.assert_middleware_usage(post_middleware, True,  True,  True,  True)
+        self.assert_middleware_usage(pre_middleware,  True,  True, False, True, False)
+        self.assert_middleware_usage(middleware,      True,  True, False, True,  True)
+        self.assert_middleware_usage(post_middleware, True,  True, False, True,  True)
 
     def test_process_request_middleware_null_view(self):
         pre_middleware = TestMiddleware()
@@ -299,9 +345,9 @@
         self.assert_exceptions_handled('/middleware_exceptions/null_view/', [])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  False, True,  False)
-        self.assert_middleware_usage(middleware,      True,  False, True,  False)
-        self.assert_middleware_usage(post_middleware, False, False, True,  False)
+        self.assert_middleware_usage(pre_middleware,  True,  False, False, True,  False)
+        self.assert_middleware_usage(middleware,      True,  False, False, True,  False)
+        self.assert_middleware_usage(post_middleware, False, False, False, True,  False)
 
     def test_process_view_middleware_null_view(self):
         pre_middleware = TestMiddleware()
@@ -313,9 +359,9 @@
         self.assert_exceptions_handled('/middleware_exceptions/null_view/', [])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
-        self.assert_middleware_usage(middleware,      True,  True,  True, False)
-        self.assert_middleware_usage(post_middleware, True,  False, True, False)
+        self.assert_middleware_usage(pre_middleware,  True, True, False, True, False)
+        self.assert_middleware_usage(middleware,      True, True, False, True, False)
+        self.assert_middleware_usage(post_middleware, True, False, False, True, False)
 
     def test_process_response_middleware_null_view(self):
         pre_middleware = TestMiddleware()
@@ -330,9 +376,9 @@
             ValueError())
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  True,  True,  False)
-        self.assert_middleware_usage(middleware,      True,  True,  True,  False)
-        self.assert_middleware_usage(post_middleware, True,  True,  True,  False)
+        self.assert_middleware_usage(pre_middleware,  True,  True, False, True, False)
+        self.assert_middleware_usage(middleware,      True,  True, False, True, False)
+        self.assert_middleware_usage(post_middleware, True,  True, False, True, False)
 
     def test_process_exception_middleware_null_view(self):
         pre_middleware = TestMiddleware()
@@ -347,9 +393,9 @@
             ValueError())
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
-        self.assert_middleware_usage(middleware,      True,  True,  True, False)
-        self.assert_middleware_usage(post_middleware, True,  True,  True, False)
+        self.assert_middleware_usage(pre_middleware,  True,  True, False,  True, False)
+        self.assert_middleware_usage(middleware,      True,  True, False,  True, False)
+        self.assert_middleware_usage(post_middleware, True,  True, False,  True, False)
 
     def test_process_request_middleware_permission_denied(self):
         pre_middleware = TestMiddleware()
@@ -361,9 +407,9 @@
         self.assert_exceptions_handled('/middleware_exceptions/permission_denied/', [])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  False, True,  False)
-        self.assert_middleware_usage(middleware,      True,  False, True,  False)
-        self.assert_middleware_usage(post_middleware, False, False, True,  False)
+        self.assert_middleware_usage(pre_middleware,  True,  False, False, True,  False)
+        self.assert_middleware_usage(middleware,      True,  False, False, True,  False)
+        self.assert_middleware_usage(post_middleware, False, False, False, True,  False)
 
     def test_process_view_middleware_permission_denied(self):
         pre_middleware = TestMiddleware()
@@ -375,9 +421,9 @@
         self.assert_exceptions_handled('/middleware_exceptions/permission_denied/', [])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
-        self.assert_middleware_usage(middleware,      True,  True,  True, False)
-        self.assert_middleware_usage(post_middleware, True,  False, True, False)
+        self.assert_middleware_usage(pre_middleware,  True,  True, False,  True, False)
+        self.assert_middleware_usage(middleware,      True,  True, False,  True, False)
+        self.assert_middleware_usage(post_middleware, True,  False, False, True, False)
 
     def test_process_response_middleware_permission_denied(self):
         pre_middleware = TestMiddleware()
@@ -389,9 +435,9 @@
         self.assert_exceptions_handled('/middleware_exceptions/permission_denied/', [])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  True,  True,  True)
-        self.assert_middleware_usage(middleware,      True,  True,  True,  True)
-        self.assert_middleware_usage(post_middleware, True,  True,  True,  True)
+        self.assert_middleware_usage(pre_middleware,  True,  True, False,  True,  True)
+        self.assert_middleware_usage(middleware,      True,  True, False,  True,  True)
+        self.assert_middleware_usage(post_middleware, True,  True, False,  True,  True)
 
     def test_process_exception_middleware_permission_denied(self):
         pre_middleware = TestMiddleware()
@@ -403,9 +449,9 @@
         self.assert_exceptions_handled('/middleware_exceptions/permission_denied/', [])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
-        self.assert_middleware_usage(middleware,  True,  True,  True,  True)
-        self.assert_middleware_usage(post_middleware, True,  True,  True,  True)
+        self.assert_middleware_usage(pre_middleware,  True,  True, False,  True, False)
+        self.assert_middleware_usage(middleware,  True,  True, False,  True,  True)
+        self.assert_middleware_usage(post_middleware, True,  True, False,  True,  True)
 
 class BadMiddlewareTests(BaseMiddlewareExceptionTest):
 
@@ -419,9 +465,9 @@
         self.assert_exceptions_handled('/middleware_exceptions/view/', ['Test Request Exception'])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  False, True,  False)
-        self.assert_middleware_usage(bad_middleware,  True,  False, True,  False)
-        self.assert_middleware_usage(post_middleware, False, False, True,  False)
+        self.assert_middleware_usage(pre_middleware,  True,  False, False, True,  False)
+        self.assert_middleware_usage(bad_middleware,  True,  False, False, True,  False)
+        self.assert_middleware_usage(post_middleware, False, False, False, True,  False)
 
     def test_process_view_bad_middleware(self):
         pre_middleware = TestMiddleware()
@@ -433,9 +479,23 @@
         self.assert_exceptions_handled('/middleware_exceptions/view/', ['Test View Exception'])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
-        self.assert_middleware_usage(bad_middleware,  True,  True,  True, False)
-        self.assert_middleware_usage(post_middleware, True,  False, True, False)
+        self.assert_middleware_usage(pre_middleware,  True,  True, False,  True, False)
+        self.assert_middleware_usage(bad_middleware,  True,  True, False,  True, False)
+        self.assert_middleware_usage(post_middleware, True,  False, False, True, False)
+
+    def test_process_template_response_bad_middleware(self):
+        pre_middleware = TestMiddleware()
+        bad_middleware = BadTemplateResponseMiddleware()
+        post_middleware = TestMiddleware()
+        self._add_middleware(post_middleware)
+        self._add_middleware(bad_middleware)
+        self._add_middleware(pre_middleware)
+        self.assert_exceptions_handled('/middleware_exceptions/template_response/', ['Test Template Response Exception'])
+
+        # Check that the right middleware methods have been invoked
+        self.assert_middleware_usage(pre_middleware,  True,  True, False,  False, False)
+        self.assert_middleware_usage(bad_middleware,  True,  True, True,  False,  False)
+        self.assert_middleware_usage(post_middleware, True,  True, True,  False,  False)
 
     def test_process_response_bad_middleware(self):
         pre_middleware = TestMiddleware()
@@ -447,9 +507,9 @@
         self.assert_exceptions_handled('/middleware_exceptions/view/', ['Test Response Exception'])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  True,  False, False)
-        self.assert_middleware_usage(bad_middleware,  True,  True,  True,  False)
-        self.assert_middleware_usage(post_middleware, True,  True,  True,  False)
+        self.assert_middleware_usage(pre_middleware,  True,  True, False,  False, False)
+        self.assert_middleware_usage(bad_middleware,  True,  True, False,  True,  False)
+        self.assert_middleware_usage(post_middleware, True,  True, False,  True,  False)
 
     def test_process_exception_bad_middleware(self):
         pre_middleware = TestMiddleware()
@@ -461,9 +521,9 @@
         self.assert_exceptions_handled('/middleware_exceptions/view/', [])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
-        self.assert_middleware_usage(bad_middleware,  True,  True,  True, False)
-        self.assert_middleware_usage(post_middleware, True,  True,  True, False)
+        self.assert_middleware_usage(pre_middleware,  True,  True, False,  True, False)
+        self.assert_middleware_usage(bad_middleware,  True,  True, False,  True, False)
+        self.assert_middleware_usage(post_middleware, True,  True, False,  True, False)
 
     def test_process_request_bad_middleware_not_found(self):
         pre_middleware = TestMiddleware()
@@ -475,9 +535,9 @@
         self.assert_exceptions_handled('/middleware_exceptions/not_found/', ['Test Request Exception'])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  False, True,  False)
-        self.assert_middleware_usage(bad_middleware,  True,  False, True,  False)
-        self.assert_middleware_usage(post_middleware, False, False, True,  False)
+        self.assert_middleware_usage(pre_middleware,  True,  False, False, True,  False)
+        self.assert_middleware_usage(bad_middleware,  True,  False, False, True,  False)
+        self.assert_middleware_usage(post_middleware, False, False, False, True,  False)
 
     def test_process_view_bad_middleware_not_found(self):
         pre_middleware = TestMiddleware()
@@ -489,9 +549,9 @@
         self.assert_exceptions_handled('/middleware_exceptions/not_found/', ['Test View Exception'])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
-        self.assert_middleware_usage(bad_middleware,  True,  True,  True, False)
-        self.assert_middleware_usage(post_middleware, True,  False, True, False)
+        self.assert_middleware_usage(pre_middleware,  True,  True, False,  True, False)
+        self.assert_middleware_usage(bad_middleware,  True,  True, False,  True, False)
+        self.assert_middleware_usage(post_middleware, True,  False, False, True, False)
 
     def test_process_response_bad_middleware_not_found(self):
         pre_middleware = TestMiddleware()
@@ -503,9 +563,9 @@
         self.assert_exceptions_handled('/middleware_exceptions/not_found/', ['Test Response Exception'])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  True,  False, True)
-        self.assert_middleware_usage(bad_middleware,  True,  True,  True,  True)
-        self.assert_middleware_usage(post_middleware, True,  True,  True,  True)
+        self.assert_middleware_usage(pre_middleware,  True,  True, False,  False, True)
+        self.assert_middleware_usage(bad_middleware,  True,  True, False,  True,  True)
+        self.assert_middleware_usage(post_middleware, True,  True, False,  True,  True)
 
     def test_process_exception_bad_middleware_not_found(self):
         pre_middleware = TestMiddleware()
@@ -517,9 +577,9 @@
         self.assert_exceptions_handled('/middleware_exceptions/not_found/', ['Test Exception Exception'])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
-        self.assert_middleware_usage(bad_middleware,  True,  True,  True,  True)
-        self.assert_middleware_usage(post_middleware, True,  True,  True,  True)
+        self.assert_middleware_usage(pre_middleware,  True,  True, False,  True, False)
+        self.assert_middleware_usage(bad_middleware,  True,  True, False,  True,  True)
+        self.assert_middleware_usage(post_middleware, True,  True, False,  True,  True)
 
     def test_process_request_bad_middleware_exception(self):
         pre_middleware = TestMiddleware()
@@ -531,9 +591,9 @@
         self.assert_exceptions_handled('/middleware_exceptions/error/', ['Test Request Exception'])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  False, True,  False)
-        self.assert_middleware_usage(bad_middleware,  True,  False, True,  False)
-        self.assert_middleware_usage(post_middleware, False, False, True,  False)
+        self.assert_middleware_usage(pre_middleware,  True,  False, False, True,  False)
+        self.assert_middleware_usage(bad_middleware,  True,  False, False, True,  False)
+        self.assert_middleware_usage(post_middleware, False, False, False, True,  False)
 
     def test_process_view_bad_middleware_exception(self):
         pre_middleware = TestMiddleware()
@@ -545,9 +605,9 @@
         self.assert_exceptions_handled('/middleware_exceptions/error/', ['Test View Exception'])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
-        self.assert_middleware_usage(bad_middleware,  True,  True,  True, False)
-        self.assert_middleware_usage(post_middleware, True,  False, True, False)
+        self.assert_middleware_usage(pre_middleware,  True,  True, False,  True, False)
+        self.assert_middleware_usage(bad_middleware,  True,  True, False,  True, False)
+        self.assert_middleware_usage(post_middleware, True,  False, False, True, False)
 
     def test_process_response_bad_middleware_exception(self):
         pre_middleware = TestMiddleware()
@@ -559,9 +619,9 @@
         self.assert_exceptions_handled('/middleware_exceptions/error/', ['Error in view', 'Test Response Exception'])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  True,  False, True)
-        self.assert_middleware_usage(bad_middleware,  True,  True,  True,  True)
-        self.assert_middleware_usage(post_middleware, True,  True,  True,  True)
+        self.assert_middleware_usage(pre_middleware,  True,  True, False,  False, True)
+        self.assert_middleware_usage(bad_middleware,  True,  True, False,  True,  True)
+        self.assert_middleware_usage(post_middleware, True,  True, False,  True,  True)
 
     def test_process_exception_bad_middleware_exception(self):
         pre_middleware = TestMiddleware()
@@ -573,9 +633,9 @@
         self.assert_exceptions_handled('/middleware_exceptions/error/', ['Test Exception Exception'])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
-        self.assert_middleware_usage(bad_middleware,  True,  True,  True,  True)
-        self.assert_middleware_usage(post_middleware, True,  True,  True,  True)
+        self.assert_middleware_usage(pre_middleware,  True,  True, False,  True, False)
+        self.assert_middleware_usage(bad_middleware,  True,  True, False,  True,  True)
+        self.assert_middleware_usage(post_middleware, True,  True, False,  True,  True)
 
     def test_process_request_bad_middleware_null_view(self):
         pre_middleware = TestMiddleware()
@@ -587,9 +647,9 @@
         self.assert_exceptions_handled('/middleware_exceptions/null_view/', ['Test Request Exception'])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  False, True,  False)
-        self.assert_middleware_usage(bad_middleware,  True,  False, True,  False)
-        self.assert_middleware_usage(post_middleware, False, False, True,  False)
+        self.assert_middleware_usage(pre_middleware,  True,  False, False, True,  False)
+        self.assert_middleware_usage(bad_middleware,  True,  False, False, True,  False)
+        self.assert_middleware_usage(post_middleware, False, False, False, True,  False)
 
     def test_process_view_bad_middleware_null_view(self):
         pre_middleware = TestMiddleware()
@@ -601,9 +661,9 @@
         self.assert_exceptions_handled('/middleware_exceptions/null_view/', ['Test View Exception'])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
-        self.assert_middleware_usage(bad_middleware,  True,  True,  True, False)
-        self.assert_middleware_usage(post_middleware, True,  False, True, False)
+        self.assert_middleware_usage(pre_middleware,  True,  True, False,  True, False)
+        self.assert_middleware_usage(bad_middleware,  True,  True, False,  True, False)
+        self.assert_middleware_usage(post_middleware, True,  False, False, True, False)
 
     def test_process_response_bad_middleware_null_view(self):
         pre_middleware = TestMiddleware()
@@ -618,9 +678,9 @@
             ])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  True,  False, False)
-        self.assert_middleware_usage(bad_middleware,  True,  True,  True,  False)
-        self.assert_middleware_usage(post_middleware, True,  True,  True,  False)
+        self.assert_middleware_usage(pre_middleware,  True,  True, False,  False, False)
+        self.assert_middleware_usage(bad_middleware,  True,  True, False,  True,  False)
+        self.assert_middleware_usage(post_middleware, True,  True, False,  True,  False)
 
     def test_process_exception_bad_middleware_null_view(self):
         pre_middleware = TestMiddleware()
@@ -635,9 +695,9 @@
             ValueError())
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
-        self.assert_middleware_usage(bad_middleware,  True,  True,  True, False)
-        self.assert_middleware_usage(post_middleware, True,  True,  True, False)
+        self.assert_middleware_usage(pre_middleware,  True,  True, False,  True, False)
+        self.assert_middleware_usage(bad_middleware,  True,  True, False,  True, False)
+        self.assert_middleware_usage(post_middleware, True,  True, False,  True, False)
 
     def test_process_request_bad_middleware_permission_denied(self):
         pre_middleware = TestMiddleware()
@@ -649,9 +709,9 @@
         self.assert_exceptions_handled('/middleware_exceptions/permission_denied/', ['Test Request Exception'])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  False, True,  False)
-        self.assert_middleware_usage(bad_middleware,  True,  False, True,  False)
-        self.assert_middleware_usage(post_middleware, False, False, True,  False)
+        self.assert_middleware_usage(pre_middleware,  True,  False, False, True,  False)
+        self.assert_middleware_usage(bad_middleware,  True,  False, False, True,  False)
+        self.assert_middleware_usage(post_middleware, False, False, False, True,  False)
 
     def test_process_view_bad_middleware_permission_denied(self):
         pre_middleware = TestMiddleware()
@@ -663,9 +723,9 @@
         self.assert_exceptions_handled('/middleware_exceptions/permission_denied/', ['Test View Exception'])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
-        self.assert_middleware_usage(bad_middleware,  True,  True,  True, False)
-        self.assert_middleware_usage(post_middleware, True,  False, True, False)
+        self.assert_middleware_usage(pre_middleware,  True,  True, False,  True, False)
+        self.assert_middleware_usage(bad_middleware,  True,  True, False,  True, False)
+        self.assert_middleware_usage(post_middleware, True,  False, False, True, False)
 
     def test_process_response_bad_middleware_permission_denied(self):
         pre_middleware = TestMiddleware()
@@ -677,9 +737,9 @@
         self.assert_exceptions_handled('/middleware_exceptions/permission_denied/', ['Test Response Exception'])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  True,  False, True)
-        self.assert_middleware_usage(bad_middleware,  True,  True,  True,  True)
-        self.assert_middleware_usage(post_middleware, True,  True,  True,  True)
+        self.assert_middleware_usage(pre_middleware,  True,  True, False,  False, True)
+        self.assert_middleware_usage(bad_middleware,  True,  True, False,  True,  True)
+        self.assert_middleware_usage(post_middleware, True,  True, False,  True,  True)
 
     def test_process_exception_bad_middleware_permission_denied(self):
         pre_middleware = TestMiddleware()
@@ -691,6 +751,6 @@
         self.assert_exceptions_handled('/middleware_exceptions/permission_denied/', ['Test Exception Exception'])
 
         # Check that the right middleware methods have been invoked
-        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
-        self.assert_middleware_usage(bad_middleware,  True,  True,  True,  True)
-        self.assert_middleware_usage(post_middleware, True,  True,  True,  True)
+        self.assert_middleware_usage(pre_middleware,  True,  True, False,  True, False)
+        self.assert_middleware_usage(bad_middleware,  True,  True, False,  True,  True)
+        self.assert_middleware_usage(post_middleware, True,  True, False,  True,  True)
diff -r 8343c0d86151 tests/regressiontests/middleware_exceptions/urls.py
--- a/tests/regressiontests/middleware_exceptions/urls.py	Tue Nov 02 13:42:30 2010 +0000
+++ b/tests/regressiontests/middleware_exceptions/urls.py	Tue Nov 02 19:57:53 2010 +0500
@@ -9,4 +9,7 @@
     (r'^error/$', views.server_error),
     (r'^null_view/$', views.null_view),
     (r'^permission_denied/$', views.permission_denied),
+
+    (r'^template_response/$', views.template_response),
+    (r'^template_response_error/$', views.template_response_error),
 )
diff -r 8343c0d86151 tests/regressiontests/middleware_exceptions/views.py
--- a/tests/regressiontests/middleware_exceptions/views.py	Tue Nov 02 13:42:30 2010 +0000
+++ b/tests/regressiontests/middleware_exceptions/views.py	Tue Nov 02 19:57:53 2010 +0500
@@ -1,9 +1,17 @@
 from django import http
 from django.core.exceptions import PermissionDenied
+from django.shortcuts import render
+from django.template import Template
 
 def normal_view(request):
     return http.HttpResponse('OK')
 
+def template_response(request):
+    return render(request, Template('OK'))
+
+def template_response_error(request):
+    return render(request, Template('{%'))
+
 def not_found(request):
     raise http.Http404()
 
diff -r 8343c0d86151 tests/regressiontests/templates/response.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/regressiontests/templates/response.py	Tue Nov 02 19:57:53 2010 +0500
@@ -0,0 +1,179 @@
+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)
diff -r 8343c0d86151 tests/regressiontests/templates/tests.py
--- a/tests/regressiontests/templates/tests.py	Tue Nov 02 13:42:30 2010 +0000
+++ b/tests/regressiontests/templates/tests.py	Tue Nov 02 19:57:53 2010 +0500
@@ -27,6 +27,7 @@
 from unicode import unicode_tests
 from nodelist import NodelistTest
 from smartif import *
+from response import *
 
 try:
     from loaders import *
diff -r 8343c0d86151 tests/regressiontests/views/templates/debug/render_test.html
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/regressiontests/views/templates/debug/render_test.html	Tue Nov 02 19:57:53 2010 +0500
@@ -0,0 +1,1 @@
+{{ foo }}.{{ bar }}.{{ baz }}.{{ processors }}
diff -r 8343c0d86151 tests/regressiontests/views/tests/__init__.py
--- a/tests/regressiontests/views/tests/__init__.py	Tue Nov 02 13:42:30 2010 +0000
+++ b/tests/regressiontests/views/tests/__init__.py	Tue Nov 02 19:57:53 2010 +0500
@@ -7,3 +7,4 @@
 from i18n import *
 from specials import *
 from static import *
+from shortcuts import *
diff -r 8343c0d86151 tests/regressiontests/views/tests/shortcuts.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/regressiontests/views/tests/shortcuts.py	Tue Nov 02 19:57:53 2010 +0500
@@ -0,0 +1,45 @@
+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')
+
