You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
20 lines
434 B
Python
20 lines
434 B
Python
import re
|
|
|
|
from django import template
|
|
from django.urls import reverse, NoReverseMatch
|
|
|
|
register = template.Library()
|
|
|
|
|
|
@register.simple_tag(takes_context=True)
|
|
def active(context, pattern_or_urlname):
|
|
try:
|
|
pattern = '^' + reverse(pattern_or_urlname)
|
|
except NoReverseMatch:
|
|
pattern = pattern_or_urlname
|
|
path = context['request'].path
|
|
if re.search(pattern, path):
|
|
return 'active'
|
|
return ''
|
|
|