Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get_absolute_url with domain in Django template?

Tags:

django

So I am struggling a bit, with something that logically seems so simple but due to my limited understanding of Django I am not sure where to look and how to formulate a solution.

Basically I have a Blog app set up and it shows the complete(all the content including disqus discussion) latest post on the home page. The post has a further link to the posts own page as well. I have set up Disqus and need to get some key information to use for the disqus_url and disqus_identifier. I have set up the model as follows with a method for get_absolute_url as follows:

def get_absolute_url(self):
    return reverse('blog.views.renderBlog',args=[str(self.id),str(self.slug)])

My view is set up as follows:

def renderBlog(request,postid=1,slug=None):
    template = 'blog_home.html'

    if(postid == 1 and slug == None):
        post = Post.objects.latest('date_created')
    else:
        post = Post.objects.get(slug=slug, id=postid)

    data = {
            'post':post,
        }

    return render(request, template, data)

As you can see the view is set up to handle both URL's as follows:

    url(r'^$', 'renderBlog', name='blogHome'),
    url(r'^post/(?P<postid>\d{1,4})/(?P<slug>[\w-]+)/$', 'renderBlog', name='blogPostPage'),

In my template I am setting disqus_identifier = '{{ post.get_absolute_url }}' and I am hardcoding the domain portion in the meantime as disqus_url = 'http://127.0.0.1{{ post.get_absolute_url }}';.. Same goes for the comment count <a href="" data-disqus-identifier.

I dont like doing things in a hackish manner, what would be the best method for me to get the full absolute url. I have looked at request.get_absolute_uri but am not sure on how to actually use it to get what I want.

Thanks

like image 306
Deep Avatar asked Jun 22 '14 15:06

Deep


1 Answers

The way I like to do it is configure a context_processor:

from django.contrib.sites.models import Site

def base_context_processor(request):
     return {
         'BASE_URL': "http://%s" % Site.objects.get_current().domain
     }
     # or if you don't want to use 'sites' app
     return {
         'BASE_URL': request.build_absolute_uri("/").rstrip("/")
     }

in settings.py:

TEMPLATE_CONTEXT_PROCESSORS = (
    ...
    'path.to.base_context_processor',
    ...
)      

(In newer versions of Django, modify context_processors under TEMPLATES, OPTIONS instead.)

then in templates:

<a href="{{ BASE_URL }}{{ obj.get_absolute_url }}">Object Name</a> 

Another solution would be to use request.build_absolute_uri(location), but then you would have to create a template tag that takes a request object and a location or an object that has get_absolute_uri method. Then you would be able to use it templates like that: {% get_full_uri request=request obj=post %}. Here is documentation on how to write custom tags.

like image 179
lehins Avatar answered Oct 20 '22 03:10

lehins