tags:

views:

20

answers:

1

I want to have a absolute/complete url when i call my models get_absolute_url method in template. in my entry model i have below:

def get_absolute_url(self):
    return ('blog_entry_detail', (), { 'year': self.pub_date.strftime("%Y"),
                                           'month': self.pub_date.strftime("%b").lower(),
                                           'day': self.pub_date.strftime("%d"),
                                           'slug': self.slug })

get_absolute_url = models.permalink(get_absolute_url)

in my template file:

{{object.get_absolute_url}}

I want to output the url prepended with 'http://www.example.com'

I want to use below lines to get the current domain name but i dont know where will i put it.

from django.contrib.sites.models import Site
current_site = Site.objects.get_current().domain
A: 

When I need full URLs, I usally go for request.get_absolute_url(), rather than assemble it myself using Site.objects.get_current().domain. However, there seem to be some issues with this method.

In any case, I don't think the model is the right place to put this because it violates the DRY principle by duplicating the URL in the urlconf and in the model. Can you not use some form of reverse URL lookups?

André Caron
can you pls give some example on how to use request.get_absolute_url()? currently i have a context processor to pass the site name variable to templates and assemble the absolute url like this:<a href="{{SITE_DOMAIN}}{{object.get_absolute_url}}">{{object.title}}</a>which i think is not a good way to output the absolute url
ronbeltran
Just pass the URL relative to your root URL confirm like 'request.get_full_url(URL_to_my_view)'. I often use that along with the reverse lookup, which is also why I links to that.
André Caron