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 6 years, 5 months ago
  2. template filter to include protocol and domain in absolute urls by czer 4 years, 1 month ago
  3. ScriptPrefixMiddleware by shellsage 6 years, 12 months ago
  4. Add site info to request context by bthomas 7 years, 9 months ago
  5. Add multiple parameters to the current url by cschand 8 years, 1 month ago

Comments

cornbread (on July 23, 2009):
<p>It does help to explain that the way to change your current site is from the admin under sites. :) great snippet btw</p>

#

junckritter (on December 10, 2009):
<p>For complete behaviour of url tag, in render method should be </p> <pre>if self.asvar: context[self.asvar]= urlparse.urljoin(domain, context[self.asvar]) return '' else: return urlparse.urljoin(domain, path) </pre> <p>instead of pure return.</p>

#

joe (on May 19, 2010):
<p>Note that the snippet in the comment by junckritter works well. Thanks junckritter!</p>

#

lvanderree (on August 23, 2011):
<p>I wrote the following, which also takes httpS into account</p> <pre>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) </pre> <p>in your templates you can then use</p> <pre>{% load absolute_url %} {% url accept-friend key as invite_url %} <p>{% absolute_url invite_url %}</p> </pre>

#

MechanisM (on December 3, 2011):
<p>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!</p>

#

Please login first before commenting.