Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
How to overwrite the hyperlink of objects in change list template of django admin?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Leon  
View profile  
 More options Apr 13 2008, 11:20 pm
From: Leon <LeonChen8...@gmail.com>
Date: Sun, 13 Apr 2008 06:20:14 -0700 (PDT)
Local: Sun, Apr 13 2008 11:20 pm
Subject: How to overwrite the hyperlink of objects in change list template of django admin?
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?

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Malcolm Tredinnick  
View profile  
 More options Apr 14 2008, 11:23 am
From: Malcolm Tredinnick <malc...@pointy-stick.com>
Date: Mon, 14 Apr 2008 11:23:09 +1000
Local: Mon, Apr 14 2008 11:23 am
Subject: Re: How to overwrite the hyperlink of objects in change list template of django admin?

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.

Regards,
Malcolm

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Leon  
View profile  
 More options Apr 14 2008, 9:04 pm
From: Leon <LeonChen8...@gmail.com>
Date: Mon, 14 Apr 2008 04:04:01 -0700 (PDT)
Local: Mon, Apr 14 2008 9:04 pm
Subject: Re: How to overwrite the hyperlink of objects in change list template of django admin?
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:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Malcolm Tredinnick  
View profile  
 More options Apr 15 2008, 11:22 am
From: Malcolm Tredinnick <malc...@pointy-stick.com>
Date: Tue, 15 Apr 2008 11:22:02 +1000
Local: Tues, Apr 15 2008 11:22 am
Subject: Re: How to overwrite the hyperlink of objects in change list template of django admin?

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/


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Leon  
View profile  
 More options Apr 15 2008, 1:50 pm
From: Leon <LeonChen8...@gmail.com>
Date: Mon, 14 Apr 2008 20:50:07 -0700 (PDT)
Local: Tues, Apr 15 2008 1:50 pm
Subject: Re: How to overwrite the hyperlink of objects in change list template of django admin?
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:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Malcolm Tredinnick  
View profile  
 More options Apr 15 2008, 2:17 pm
From: Malcolm Tredinnick <malc...@pointy-stick.com>
Date: Tue, 15 Apr 2008 14:17:44 +1000
Local: Tues, Apr 15 2008 2:17 pm
Subject: Re: How to overwrite the hyperlink of objects in change list template of django admin?

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/


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Leon  
View profile  
 More options Apr 20 2008, 5:10 pm
From: Leon <LeonChen8...@gmail.com>
Date: Sun, 20 Apr 2008 00:10:17 -0700 (PDT)
Local: Sun, Apr 20 2008 5:10 pm
Subject: Re: How to overwrite the hyperlink of objects in change list template of django admin?
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 to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google