Compare request.path with a reversed url in Django template
django, django-templates, python
Solution
You can use a custom template tag
from django import template
register = template.Library()
@register.simple_tag
def active(request, pattern):
path = request.path
if path == pattern:
return 'active'
return ''
Then usage is sort of like this in your template:
{% load my_tags %}
{% url orders_list as orders %}
<li class="{% active request orders %}">
<a href="{{ orders }}"> Orders </a>
</li>
You can modify the template tag as you wish.
Problem
I understand that `request.path` will give me the current URL. I am currently working on my `base.html` template with CSS tabs and I want the template to know which tab is currently "active" and pass `class="active-tab"` to an `<a>` tag. So I wanted to do something like ``` <a href="{% url orders_list %}" {% if request.path = reverse('orders_list') %} class="active-tab" {$ endif %} >Orders</a> ``` But I'm sure you can't do that `if` comparison. I also only want the base (?) URL ignoring any GET parameters. Any suggestions or tips also welcomed. Thanks in advance!