Asterisk
Asterisk

Reputation: 3574

Highlight dropdown menu in django

I have a template with a dropdown menu. Dropdown contains several options (read links). When one of them is chosen I want the dropdown to be highlighted. To do this I want to check if request path is one of the dropdown menu options.

I.e. say my links inside the dropdown have the urls url1, url2, url2, and I want to do this in template:

{% if request.path in [url1, url2, url3] %}
    highlight dropdown menu
{% endif %}

What is the best approach to the problem?

Upvotes: 0

Views: 311

Answers (2)

Asterisk
Asterisk

Reputation: 3574

Using Samuele's answer I created a filter like this one:

@register.filter
def check(url, url_list, delimeter=","):
    url_list = url_list.split(delimeter)
    for item in url_list:
        if url in item:
            return True
    return False

Then in my template I use the following code:

{% if request.path|check:"url1,url2" %}
    # here goes the html code
{% endif %}

Upvotes: 0

Samuele Mattiuzzo
Samuele Mattiuzzo

Reputation: 11048

you can create your simple tag and use it to highlight the dropdown:

@register.simple_tag
def check(url):
    for elem in url_list:
        if elem in url:
            return true
    return false

and then apply it to your template:

<select {% if check request.get_full_path %}class="highlighted"{% endif %} >
....
</select>

Upvotes: 1

Related Questions