Clickable url in the admin

1,668 views
Skip to first unread message

Zeynel

unread,
Dec 14, 2009, 1:16:20 PM12/14/09
to Django users
In my models I have this field

firm_url = models.CharField('Bio', max_length=200)

that should have a link to the website where the lawyer bio is. This
is in the admin. I thought that

firm_url = models.URLField('Bio', max_length=200)

would make the url appear as a link; but it doesn't; documentation
says URLField is a CharField. So how do I make this url clickable in
the admin? Thanks.

Zeynel

unread,
Dec 14, 2009, 1:52:53 PM12/14/09
to Django users
I found this thread: http://groups.google.com/group/django-users/msg/11b034152c9dff1a
Does anyone have more details on urlize?

rebus_

unread,
Dec 14, 2009, 6:41:10 PM12/14/09
to django...@googlegroups.com
Create method on your model that generates anchor (html link tag)
which points to wanted URL and put that methods name in list_display
property of AdminModel object in your admin.py

http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display

Zeynel

unread,
Dec 22, 2009, 3:59:16 PM12/22/09
to Django users
Thank you. Can you give me more detailed instructions about how to do
this? I couldn't parse the referenced section of the documentation.
How do I create the method "to generate anchor?" Where does the
"method name" go in the admin.py?

Thanks again.

On Dec 14, 6:41 pm, rebus_ <r.dav...@gmail.com> wrote:
> Create method on your model that generates anchor (html link tag)
> which points to wanted URL and put that methods name in list_display
> property of AdminModel object in your admin.py
>

> http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contri...

rebus_

unread,
Dec 22, 2009, 7:31:52 PM12/22/09
to django...@googlegroups.com
2009/12/22 Zeynel <azey...@gmail.com>:

Hey,

Well, i was really short on my explanation there so let me elaborate.
I assume you have knowledge on what AdminModel is and what
list_display is used for.

Lets say you have model such as:

class SomeModel(models.Model):
somefield = models.CharField(max_length=20)
url = models.CharField(max_length=100)

def some_url(self):
""" This returns a HTML anchor (hyperlink) to somewhere """
return u'<a href="%s">Link</a>' % self.url
some_url.allow_tags = True

This model has some_url method defined which returns a plain HTML
link. When you create your AdminModel all you need to do is add the
name of them method to list_display attribute.

class SomeModelAdmin(admin.ModelAdmin):
list_display = ('somefield', 'some_url')
site.register(SomeModel, SomeModelAdmin)

Using list_display attribute of AdminModel you can choose which fields
are to be display on change list in admin for that model.

So when the admin app parses the list_display attribute on AdminModel,
for each item in the sequence it tries to see if it is a field on
model, callable on Model or callable on AdminModel.

So what you need to do is create a method on your model that returns
the complete HTML link to lawyers bio and put that methods name in
list_display and your set to go.

Also notice the "some_url.allow_tags = True" under the method
definition. This tells Django's admin that it's ok not to encode HTML
tags. Without this the returned string would not be clickable but
rather shown as plain text.

Hope this helps.

Davor

Reply all
Reply to author
Forward
0 new messages