[Solved]-Add custom Django admin action

5👍

Since custom admin views are basically just normal views there aren’t many specialities. A few things you should consider for a cleaner integration:

  • Add the url pattern through get_urls() of the AdminSite.
  • Provide the current_app argument to RequestContext or Context when rendering a template which subclasses admin templates.

EDIT:

Found a an example here:
http://shrenikp.webs.com/apps/blog/show/5211522-add-custom-view-method-for-django-admin-model-

Note, that the example doesn’t utilize the current_app argument i’ve mentioned. I suppose your view to generate the PDF just returns a HttpResponse with the appropriate content type rather than rendering a response with a Context, so it’s not needed. All in all the current_app only makes sense when you subclass an admin template used by your custom view which actually makes use of current_app somewhere.

The example encapsulates the urls and view in a ModelAdmin. It is also possible to do this through a AdminSite subclass, but at least in your use case this is probably overkill.

By the way, overriding the change_form.html template for your app to add a button to the standard change view is just fine. There is no special api for this in the admin (unfortunately).

Leave a comment