[Fixed]-Django display list items based on URL

1👍

You can create a custom filter and use it. Something like this maybe;

# nav_active.py
import re
from django.template import Library
from django.core.urlresolvers import reverse

register = Library()

@register.filter()
def nav_active(request_path, search_path):
    # WRITE YOUR LOGIC
    return search_path in request_path

Inside the template

{% load nav_active %}
{% if request_path|nav_active:"/search/path" %}
....
{% endif %} 

Update as per your comment. From Django docs code layout section for custom template tags and filters:

The app should contain a templatetags directory, at the same level as models.py, views.py, etc. If this doesn’t already exist, create it – don’t forget the init.py file to ensure the directory is treated as a Python package.

So create a folder at same level as your view.py and name it templatetags. (Don’t forget to add __init__.py inside). At the same level of that __init__.py add your nav_active.py and it should be ready to use. Like this:

yourapp/
  __init__.py
  models.py
  views.py
  templatetags/
     __init__.py
     nav_active.py

Leave a comment