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?
On Sun, 2008-04-13 at 06:20 -0700, Leon wrote: > 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?
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.
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:
> On Sun, 2008-04-13 at 06:20 -0700, Leon wrote:
> > 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?
> 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.
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).
> 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).
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: