Skip to content

Instantly share code, notes, and snippets.

@walison17
Created June 2, 2020 01:19
Show Gist options
  • Save walison17/22c1c6a0fa492da8e5d3a622335d78be to your computer and use it in GitHub Desktop.
Save walison17/22c1c6a0fa492da8e5d3a622335d78be to your computer and use it in GitHub Desktop.
from django import template
register = template.Library()
@register.simple_tag(takes_context=True)
def active_menu(context, viewnames, css_class="active", **kwargs):
request = context.get("request")
match = request.resolver_match
views = viewnames.split("||")
for view in views:
namespace, view_name = view.split(":")
if match.url_name == view_name and namespace in match.namespaces:
return css_class
return ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment