How to overwrite the hyperlink of objects in change list template of django admin?

582 views
Skip to first unread message

Leon

unread,
Apr 13, 2008, 9:20:14 AM4/13/08
to Django users
Hi,
I use django admin to manage a table. There is a hyperlink for
each object in the change_list template, which will bring me to the
change_form template. I want to change that hyperlink and redirect it
to another url to handle. I didn't want to customize the current
change_form for this model because it is still used somewhere else.
Anybody knows how to do it?

Malcolm Tredinnick

unread,
Apr 13, 2008, 9:23:09 PM4/13/08
to django...@googlegroups.com

It's possible, but fairly fiddly. You can create a custom
change_list.html template for just that application + model name
combination (since the admin interface tries to load a template under
admin/<app_name>/<model_name>/change_list.html as one of the options for
that page). Then you need to write a template that displays what you
would like, which possibly means duplicating a lot of the logic of the
original page and the template tags that construct it. With a bit of
tracing through the code (admin/templates/admin/change_list.html,
admin/tempates/admin/change_list_result.html,
admin/templatetags/admin_list.py, admin/views/main.py) it should be
quite possible to achieve what you want. Take your time and you'll get
there (or use newforms-admin or wait for newforms-admin to be merged
into trunk, both of which will be easier).

Note that this sort of customisation does require you to read some
Python code and templates and do a bit of design work. There isn't a
step-by-step guide, so if you aren't up to being able to read the code a
bit, this probably isn't the right sort of customisation to be trying to
make.

Regards,
Malcolm

--
Tolkien is hobbit-forming.
http://www.pointy-stick.com/blog/

Leon

unread,
Apr 14, 2008, 7:04:01 AM4/14/08
to Django users
I've tried that method and make my own object list in the customized
change_list.html.
That REALLY duplicate a lot of works. Is it possible to add a hook
function (middleware?) just
before the change_list template being rendered? In that way, I might
be able to modify the
data before it is passed to template.

I had expected there is a magic function in model class to change it. :
(

On Apr 14, 9:23 am, Malcolm Tredinnick <malc...@pointy-stick.com>
wrote:

Malcolm Tredinnick

unread,
Apr 14, 2008, 9:22:02 PM4/14/08
to django...@googlegroups.com

On Mon, 2008-04-14 at 04:04 -0700, Leon wrote:
> I've tried that method and make my own object list in the customized
> change_list.html.
> That REALLY duplicate a lot of works. Is it possible to add a hook
> function (middleware?) just
> before the change_list template being rendered?

You're really asking, in general, whether it's possible to make the
admin interface more customisable. Yes there is and through the
advantage of owning a time machine, we've already done it in the
newforms-admin branch. Not sure if your precise problem is addressed
specifically there, but most customisations are easier. That's on track
to be merged with trunk "soon", so it's not worth worrying too much
about anything with existing admin beyond just getting something to work
at the moment.

> In that way, I might
> be able to modify the
> data before it is passed to template.
>
> I had expected there is a magic function in model class to change it. :
> (

Why? Models represent data storage not presentation. The new admin
actually moves all of the admin stuff out of the model as part of being
more consistent in that area (along with a bunch of other benefits like
allowing multiple admin setups for a model).

Regards,
Malcolm

--
Always try to be modest and be proud of it!
http://www.pointy-stick.com/blog/

Leon

unread,
Apr 14, 2008, 11:50:07 PM4/14/08
to Django users
Great. I'll wait for the newforms-admin branch integration.

Is there any document about this newforms-admin?

On Apr 15, 9:22 am, Malcolm Tredinnick <malc...@pointy-stick.com>
wrote:

Malcolm Tredinnick

unread,
Apr 15, 2008, 12:17:44 AM4/15/08
to django...@googlegroups.com

On Mon, 2008-04-14 at 20:50 -0700, Leon wrote:
> Great. I'll wait for the newforms-admin branch integration.
>
> Is there any document about this newforms-admin?

Django's wiki and documentation and things really are very
search-engine-friendly. :-)

http://www.google.com/search?q=django+newforms-admin

The wiki page is probably the right place to start:
http://code.djangoproject.com/wiki/NewformsAdminBranch

Regards,
Malcolm

--
Honk if you love peace and quiet.
http://www.pointy-stick.com/blog/

Leon

unread,
Apr 20, 2008, 3:10:17 AM4/20/08
to Django users
I had worked out my way.

The hyperlink for a specifc object in admin interface is actually
hardcoded in contrib.admin.views.main.py, line 766:
def url_for_result(self, result):
return "%s/" % quote(getattr(result, self.pk_attname))

so what I did, is to overwrite this method of the given changelist
object. Here is the details. Hope it could be some help to other
people.
1. Define a new tag:

from django.contrib.admin.templatetags.admin_list import result_list
from types import MethodType
from django.template import Library
register = Library()

def url_for_result_new(link_format):
return lambda self, result: link_format % getattr(result,
self.pk_attname)
# This function is a template tag for use in an overridden
change_list.html template.
def result_list_with_link(cl, link_format):
url_for_result_link = url_for_result_new(link_format)
setattr(cl, 'url_for_result', MethodType(url_for_result_link, cl))
return result_list(cl)
result_list_with_link = register.inclusion_tag("admin/
change_list_results.html")(result_list_with_link)

2. Copy the template/admin/change_list.html to template/admin/
<app_name>/<model_name>/change_list.html. Here the the <model_name> is
the one that you want to overwrite its object change hyperlink

3.Edit the template/admin/<app_name>/<model_name>/change_list.html.
Change this line:
{% block result_list %}{% result_list cl %}{% endblock %}

to:
{% load <module that defines the result_list_with_link> %>
{% block result_list %}{% result_list_with_link cl "/<other url>/%s/"
%}{% endblock %}

here the "%s" will be filled with object pk


On Apr 15, 12:17 pm, Malcolm Tredinnick <malc...@pointy-stick.com>
wrote:
Reply all
Reply to author
Forward
0 new messages