Display an URLField as a Link

1,819 views
Skip to first unread message

Tom

unread,
May 8, 2009, 3:26:59 PM5/8/09
to Django users
My model :

url = models.URLField(max_length=500,blank=True,verify_exists=False)

and I want the column url display url as a link (<a href=""></a>)

Any ideas ?

Thanks for your help.

Michael

unread,
May 8, 2009, 5:45:58 PM5/8/09
to django...@googlegroups.com
In general the models are for your database logic and display logic like this would be saved until your template. But since you insist, one easy way to accomplish this is by setting up a property on your model like so:

@property
def url_formatted(self):
     return '<a href="%s"></a>' % self.url

then you have a property on your model named url_formatted that will give you the output you desire.

I hope that helps,

Michael 

Tom

unread,
May 11, 2009, 4:29:40 PM5/11/09
to Django users
Thank you Michael,

Can you explain me the "Clean" way to do that ?

On May 8, 2:45 pm, Michael <newmani...@gmail.com> wrote:

Michael

unread,
May 11, 2009, 6:58:53 PM5/11/09
to django...@googlegroups.com
On Mon, May 11, 2009 at 4:29 PM, Tom <thomas...@gmail.com> wrote:

Thank you Michael,

Can you explain me the "Clean" way to do that ?

Sure, you create a view (or use a generic view) that calls a template and has one of your models like so:

from django.shortcuts import render_to_response
from yourapp.models import YourModel

def yourview(request):
    m = YourModel.objects.order_by('?')[0]
    return render_to_response('yourtemplate.html', { 'object' : m})

and your template would then have a simple link like so

<a href="{{ object.url }}">{{ object.title }}</a>

Simple. I hope that helps,

Michael 

Morgan

unread,
May 11, 2009, 5:50:12 PM5/11/09
to Django users
In your template, you can use the "urlize" templatetag:

{{ mymodel.url|urlize }}

Reiner

unread,
May 12, 2009, 4:32:54 AM5/12/09
to Django users
Initially I thought you were talking about the admin, because of the
column url you mentioned. If that's the case, have a look at this part
of the documentation. You can define a property on your model, or if
you just want the admin to display it as a link, use a module-level
function or define it on your modeladmin. There are multiple ways of
accomplishing this.

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

Tom

unread,
May 26, 2009, 8:30:41 PM5/26/09
to Django users
Thanks for your help.

I used the http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display
method who work fine for me.

def url_as_link(self):
return '<a href="color: #%s;">%s</a>' % (self.url, self.url)
url_as_link.allow_tags = True

And call 'url_as_link' insted of 'url' in the ModelAdmin.

pavan dhok

unread,
Feb 18, 2022, 9:12:14 AM2/18/22
to Django users
its work for me.

# models.py #
class YourModel(models.Model):
        Website_list = models.URLField(max_length=5000)

        def url(self):
                return format_html("<a href='{url}' target='_blank'>{url}</a>", url=self.Website_list)

# admin.py #
list_display = ['url']
Reply all
Reply to author
Forward
0 new messages