Login

Absolute URL Templatetag

Author:
johnboxall
Posted:
May 21, 2009
Language:
Python
Version:
1.0
Score:
7 (after 7 ratings)

The {% url %} templatetag is awesome sometimes it is useful to get the full blown URL with the domain name - for instance for links in emails. The {% absurl %} templatetag mirrors the behaviour of {% url %} but inserts absolute URLs with the domain of the current Site object.

Usage:

{% absurl viewname %}
>>> http://www.example.org/my/view/
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import urlparse
from django.template import Library
from django.template.defaulttags import URLNode, url
from django.contrib.sites.models import Site

register = Library()

class AbsoluteURLNode(URLNode):
    def render(self, context):
        path = super(AbsoluteURLNode, self).render(context)
        domain = "http://%s" % Site.objects.get_current().domain
        return urlparse.urljoin(domain, path)

def absurl(parser, token, node_cls=AbsoluteURLNode):
    """Just like {% url %} but ads the domain of the current site."""
    node_instance = url(parser, token)
    return node_cls(view_name=node_instance.view_name,
        args=node_instance.args,
        kwargs=node_instance.kwargs,
        asvar=node_instance.asvar)
absurl = register.tag(absurl)        

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

cornbread (on July 23, 2009):

It does help to explain that the way to change your current site is from the admin under sites. :) great snippet btw

#

junckritter (on December 10, 2009):

For complete behaviour of url tag, in render method should be

if self.asvar:  
    context[self.asvar]= urlparse.urljoin(domain, context[self.asvar])  
    return ''  
else:  
    return urlparse.urljoin(domain, path)

instead of pure return.

#

joe (on May 19, 2010):

Note that the snippet in the comment by junckritter works well. Thanks junckritter!

#

lvanderree (on August 23, 2011):

I wrote the following, which also takes httpS into account

from django import template
register = template.Library()

class UrlNode(template.Node):
    def __init__(self, path=None):
        self.path = path

    def render(self, context):
        request = context['request']

        return request.build_absolute_uri(self.path)

@register.tag
def absolute_url(parser, token):
    """
    returns an absolute url to this page, or optionally pass a path

    Syntax::
        {% absolute_url [path] %}
    """

    path = None

    bits = token.split_contents()
    if len(bits) >= 2:
        path = bits[1]

    return UrlNode(path)

in your templates you can then use

{% load absolute_url %}
{% url accept-friend key as invite_url %}
<p>{% absolute_url invite_url %}</p>

#

MechanisM (on December 3, 2011):

Using query to database just to get domain name of current site is bad idea for me. I prefer gettin domain name value in settings. But thanxx anyway!

#

Please login first before commenting.