Login

Absolute URL Templatetag

Author:
johnboxall
Posted:
May 21, 2009
Language:
Python
Version:
1.0
Tags:
url templatetags absolute uri
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. External service Test Client by theju 5 years, 4 months ago
  2. template filter to include protocol and domain in absolute urls by czer 2 years, 12 months ago
  3. ScriptPrefixMiddleware by shellsage 5 years, 10 months ago
  4. Add site info to request context by bthomas 6 years, 7 months ago
  5. Add multiple parameters to the current url by cschand 7 years 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.