Alex Rivera | Logout

django full url in get_absolute_url

Asked 2010-10-22T05:10:23.740
10

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
Edit
Report

2 Answers

4

This might work for you:

class Article(models.Model):
    ...
    ...
    def get_absolute_url(self):
        path = reverse('display_article', args=[self.slug])
        return "http://%s%s" % (self.site, path)
answered 2012-09-10T18:45:50.817
1

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?

answered 2010-10-22T05:22:59.627

Your Answer