Skip to content

Instantly share code, notes, and snippets.

@rouge8
Last active December 14, 2015 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rouge8/5167189 to your computer and use it in GitHub Desktop.
Save rouge8/5167189 to your computer and use it in GitHub Desktop.
active links
from django import template
register = template.Library()
@register.filter
def startswith(value, arg):
"""Usage, {% if value|starts_with:"arg" %}"""
return value.startswith(arg)
<ul class="nav">
{% if user.is_authenticated %}
{% url app1:view as view1 %}
<li {% if request.path|startswith:view1 %}class="active"{% endif %}>
<a href="{{ view1 }}">View1</a>
</li>
{% url app2:view as view2 %}
<li {% if request.path|startswith:view2 %}class="active"{% endif %}>
<a href="{{ view2 }}">View2</a>
</li>
{% endif %}
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment