Skip to content

Instantly share code, notes, and snippets.

@tibbiyelininja
Last active January 12, 2017 09:48
Show Gist options
  • Save tibbiyelininja/69aec1259131b5619fb7 to your computer and use it in GitHub Desktop.
Save tibbiyelininja/69aec1259131b5619fb7 to your computer and use it in GitHub Desktop.
Django Absolute URL Node with Active Property
from django.template import Library
from django.template.defaulttags import URLNode, url
register = Library()
class AbsoluteURL(str):
pass
class AbsoluteURLNode(URLNode):
def render(self, context):
asvar, self.asvar = self.asvar, None
path = super(AbsoluteURLNode, self).render(context)
request_obj = context['request']
abs_url = AbsoluteURL(request_obj.build_absolute_uri(path))
if not asvar:
return str(abs_url)
else:
if path == request_obj.path:
abs_url.active = 'active'
else:
abs_url.active = ''
context[asvar] = abs_url
return ''
@register.tag
def absurl(parser, token):
node = url(parser, token)
return AbsoluteURLNode(
view_name=node.view_name,
args=node.args,
kwargs=node.kwargs,
asvar=node.asvar
)
@JellisHeRo
Copy link

Got here through a stackexchange question that you answered. If you dont mind me asking, I noticed this example u placed

{% absurl 'some-view' with, arguments %}

but I dont know what kind of arguments I would need to add onto the tag. Can you provide me with some examples?

@tibbiyelininja
Copy link
Author

@JellisHeRo as same as default url tag arguments. Sorry for late response, I am posting this with assuming that you have not found the answer yet. Just replace your url tags in your template with absurl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment