Code

Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#12292 closed (wontfix)

render_to_response method on AdminSite and ModelAdmin

Reported by: LeafStorm Owned by: nobody
Component: contrib.admin Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

I have written a patch against trunk to add a render_to_response method to AdminSite and ModelAdmin. The method replaces the

context_instance = template.RequestContext(request, current_app=self.name)
return render_to_response(templates, context, context_instance=context_instance)

idiom at the end of admin views with:

return self.render_to_response(request, templates, context)

This may seem like a small change, but it allows the method in which admin templates are rendered to be overridden. This can be used to inject extra values into the context for customized templates, or to possibly replace the Django template language with an external template language like Jinja2 or Mako in the admin ("loose coupling"). It also reduces the boilerplate in the admin and makes it easier to upgrade in the event that the template API changes ("don't repeat yourself").

The included patch includes documentation updates, but no code outside the admin has been changed (so the rest of the trunk may need updating to use the new method). All admin-related tests have been run on the patch, and it is very unlikely to break backwards compatibility, due to the fact that it merely places the code that was used before in another place.

Attachments (1)

admin-render-to-response.diff (9.5 KB) - added by LeafStorm 4 years ago.

Download all attachments as: .zip

Change History (3)

Changed 4 years ago by LeafStorm

comment:1 Changed 4 years ago by russellm

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to wontfix
  • Status changed from new to closed

I'm marking this wontfix. I'm not rejecting the idea of using non-Django template languages - just suggesting that there is a better way. Specifically, #6262/r11862 should make it possible to write a Jinja/Mako template loader without needing any changes to Django core. The process for using non-Django template languages is documented in abstract terms, but the abstract example should map fairly closely to the Jinja experience:

http://docs.djangoproject.com/en/dev/ref/templates/api/#using-an-alternative-template-language

comment:2 Changed 4 years ago by slurms

I kind of saw it as a neat way to add some extra context to a ModelAdmin..

you could do:

class FooAdmin(admin.ModelAdmin):
    def render_to_response(request, template, context):
        context.update({'foo': 'bar'})
        return super(FooAdmin, self).render_to_response(request, template, context)

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.