Comparing numbered urls in django template
"In urls.py I have:
path("viewer/<str:case>", views.viewer, name="viewer"),
This works when I go to the viewer:
<a class="nav-link dropdown-toggle {% if request.resolver_match.url_name == "viewer" %}active{% endif %}">
Now, there is a submenu in nav bar that lists cases. I need to know which specific page I'm on to make one of menu items active:
{% for item in cases %}
<li>
<a class="dropdown-item {% if request.get_full_path == "/viewer/{{ item.id }}" %}active{% endif %}" href="/viewer/{{ item.id }}">{{ item.patient_name }}</a>
</li>
request.get_full_path returns /viewer/47 for example and one of the item's id is 47. I've tried different combinations instead of "/viewer/{{ item.id }}", nothing works.
I ended up creating a custom template tag:
tags.py:
@register.simple_tag
def url_case_id(value):
return int(value.split("/")[2])
and using it in html:
{% for item in cases %}
{% url_case_id request.get_full_path as id %}
<li>
<a class="dropdown-item {% if id == item.id %}active{% endif %}" href="/viewer/{{ item.id }}">{{ item.name }}</a>
</li>