Ticket #12292: admin-render-to-response.diff

File admin-render-to-response.diff, 9.5 KB (added by LeafStorm, 15 years ago)
  • django/contrib/admin/options.py

     
    1 from django import forms, template
     1from django import forms
    22from django.forms.formsets import all_valid
    33from django.forms.models import modelform_factory, modelformset_factory, inlineformset_factory
    44from django.forms.models import BaseInlineFormSet
     
    376376        for inline in self.inline_instances:
    377377            yield inline.get_formset(request, obj)
    378378
     379    def render_to_response(self, request, templates, context):
     380        return self.admin_site.render_to_response(request, templates, context)
     381
    379382    def log_addition(self, request, object):
    380383        """
    381384        Log that an object has been successfully added.
     
    584587            'save_on_top': self.save_on_top,
    585588            'root_path': self.admin_site.root_path,
    586589        })
    587         context_instance = template.RequestContext(request, current_app=self.admin_site.name)
    588         return render_to_response(self.change_form_template or [
     590        return self.render_to_response(request, self.change_form_template or [
    589591            "admin/%s/%s/change_form.html" % (app_label, opts.object_name.lower()),
    590592            "admin/%s/change_form.html" % app_label,
    591593            "admin/change_form.html"
    592         ], context, context_instance=context_instance)
     594        ], context)
    593595
    594596    def response_add(self, request, obj, post_url_continue='../%s/'):
    595597        """
     
    907909            # the 'invalid=1' parameter was already in the query string, something
    908910            # is screwed up with the database, so display an error page.
    909911            if ERROR_FLAG in request.GET.keys():
    910                 return render_to_response('admin/invalid_setup.html', {'title': _('Database error')})
     912                return self.render_to_response(request,
     913                    'admin/invalid_setup.html', {'title': _('Database error')})
    911914            return HttpResponseRedirect(request.path + '?' + ERROR_FLAG + '=1')
    912915
    913916        # If the request was POSTed, this might be a bulk action or a bulk edit.
     
    983986            'actions_on_bottom': self.actions_on_bottom,
    984987        }
    985988        context.update(extra_context or {})
    986         context_instance = template.RequestContext(request, current_app=self.admin_site.name)
    987         return render_to_response(self.change_list_template or [
     989        return self.render_to_response(request, self.change_list_template or [
    988990            'admin/%s/%s/change_list.html' % (app_label, opts.object_name.lower()),
    989991            'admin/%s/change_list.html' % app_label,
    990992            'admin/change_list.html'
    991         ], context, context_instance=context_instance)
     993        ], context)
    992994
    993995    @csrf_protect
    994996    def delete_view(self, request, object_id, extra_context=None):
     
    10401042            "app_label": app_label,
    10411043        }
    10421044        context.update(extra_context or {})
    1043         context_instance = template.RequestContext(request, current_app=self.admin_site.name)
    1044         return render_to_response(self.delete_confirmation_template or [
     1045        return self.render_to_response(request,
     1046            self.delete_confirmation_template or [
    10451047            "admin/%s/%s/delete_confirmation.html" % (app_label, opts.object_name.lower()),
    10461048            "admin/%s/delete_confirmation.html" % app_label,
    10471049            "admin/delete_confirmation.html"
    1048         ], context, context_instance=context_instance)
     1050        ], context)
    10491051
    10501052    def history_view(self, request, object_id, extra_context=None):
    10511053        "The 'history' admin view for this model."
     
    10681070            'app_label': app_label,
    10691071        }
    10701072        context.update(extra_context or {})
    1071         context_instance = template.RequestContext(request, current_app=self.admin_site.name)
    1072         return render_to_response(self.object_history_template or [
     1073        return self.render_to_response(request, self.object_history_template or [
    10731074            "admin/%s/%s/object_history.html" % (app_label, opts.object_name.lower()),
    10741075            "admin/%s/object_history.html" % app_label,
    10751076            "admin/object_history.html"
    1076         ], context, context_instance=context_instance)
     1077        ], context)
    10771078
    10781079    #
    10791080    # DEPRECATED methods.
  • django/contrib/admin/sites.py

     
    11import re
    2 from django import http, template
     2from django import http
    33from django.contrib.admin import ModelAdmin
    44from django.contrib.admin import actions
    55from django.contrib.auth import authenticate, login
     
    88from django.core.exceptions import ImproperlyConfigured
    99from django.core.urlresolvers import reverse
    1010from django.shortcuts import render_to_response
     11from django.template import RequestContext
    1112from django.utils.functional import update_wrapper
    1213from django.utils.safestring import mark_safe
    1314from django.utils.text import capfirst
     
    270271            from django.views.i18n import null_javascript_catalog as javascript_catalog
    271272        return javascript_catalog(request, packages='django.conf')
    272273
     274    def render_to_response(self, request, templates, context):
     275        context_instance = RequestContext(request, current_app=self.name)
     276        return render_to_response(templates, context,
     277                                  context_instance=context_instance)
     278
    273279    def logout(self, request):
    274280        """
    275281        Logs out the user for the given HttpRequest.
     
    376382            'root_path': self.root_path,
    377383        }
    378384        context.update(extra_context or {})
    379         context_instance = template.RequestContext(request, current_app=self.name)
    380         return render_to_response(self.index_template or 'admin/index.html', context,
    381             context_instance=context_instance
    382         )
     385        return self.render_to_response(request,
     386            self.index_template or 'admin/index.html', context)
    383387    index = never_cache(index)
    384388
    385389    def display_login_form(self, request, error_message='', extra_context=None):
     
    391395            'root_path': self.root_path,
    392396        }
    393397        context.update(extra_context or {})
    394         context_instance = template.RequestContext(request, current_app=self.name)
    395         return render_to_response(self.login_template or 'admin/login.html', context,
    396             context_instance=context_instance
    397         )
     398        return self.render_to_response(request,
     399            self.login_template or 'admin/login.html', context)
    398400
    399401    def app_index(self, request, app_label, extra_context=None):
    400402        user = request.user
     
    435437            'root_path': self.root_path,
    436438        }
    437439        context.update(extra_context or {})
    438         context_instance = template.RequestContext(request, current_app=self.name)
    439         return render_to_response(self.app_index_template or ('admin/%s/app_index.html' % app_label,
    440             'admin/app_index.html'), context,
    441             context_instance=context_instance
    442         )
     440        return self.render_to_response(request,
     441            self.app_index_template or ('admin/%s/app_index.html' % app_label,
     442            'admin/app_index.html'), context)
    443443
    444444    def root(self, request, url):
    445445        """
  • docs/ref/contrib/admin/index.txt

     
    820820This uses the ``HttpRequest`` instance to filter the ``Car`` foreign key field
    821821to only the cars owned by the ``User`` instance.
    822822
     823.. method:: ModelAdmin.render_to_response(self, request, templates, context)
     824
     825.. versionadded:: 1.2
     826
     827The ``render_to_response`` method on a ``ModelAdmin`` invokes the
     828:meth:`AdminSite.render_to_response` method on its site. Overriding it at the
     829model level allows for the use of external templating languages, or injecting
     830variables into the context.
     831
    823832Other methods
    824833~~~~~~~~~~~~~
    825834
     
    14231432    admin template, should provide the ``current_app`` argument to
    14241433    ``RequestContext`` or ``Context`` when rendering the template.  It should
    14251434    be set to either ``self.name`` if your view is on an ``AdminSite`` or
    1426     ``self.admin_site.name`` if your view is on a ``ModelAdmin``.
     1435    ``self.admin_site.name`` if your view is on a ``ModelAdmin``. This is taken
     1436    care of for you if you use :meth:`AdminSite.render_to_response`.
    14271437
     1438Overriding template rendering
     1439-----------------------------
     1440
     1441.. method:: AdminSite.render_to_response(self, request, templates, context)
     1442
     1443.. versionadded:: 1.2
     1444
     1445This method is used to render templates from within the admin views. By default,
     1446it merely creates a :class:`~django.template.RequestContext` instance and
     1447invokes :func:`~django.shortcuts.render_to_response`. It can be overridden, for
     1448example, to add variables to the context of all the site's templates, or to
     1449use an external template language instead of Django's.
     1450
     1451The ``request`` argument is the :class:`~django.http.HttpRequest` for the view
     1452requesting that the template be rendered. ``templates`` is either a string
     1453representing a single template to render, or an iterable of strings. For the
     1454latter, the first template in the iterable that exists is used. ``context`` is
     1455a simple Python dictionary to be used as the template's context.
     1456
    14281457.. _admin-reverse-urls:
    14291458
    14301459Reversing Admin URLs
Back to Top