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)
Comments
It does help to explain that the way to change your current site is from the admin under sites. :) great snippet btw
#
For complete behaviour of url tag, in render method should be
instead of pure return.
#
Note that the snippet in the comment by junckritter works well. Thanks junckritter!
#
I wrote the following, which also takes httpS into account
in your templates you can then use
#
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!
#