Skip to main content
https://badge.fury.io/py/django-navtag.svg https://codecov.io/gh/SmileyChris/django-navtag/branch/master/graph/badge.svg

A simple Django template tag to handle navigation item selection.

Installation

Install the package using pip:

pip install django-navtag

Usage

Add the app to your INSTALLED_APPS setting:

INSTALLED_APPS = (
    # ...
    'django_navtag',
)

Give your base template a navigation block something like this:

{% load navtag %}

{% block nav %}
{% nav text ' class="selected"' %}
<ul class="nav">
    <li{{ nav.home }}><a href="/">Home</a></li>
    <li{{ nav.about }}><a href="/about/">About</a></li>
</ul>
{% endblock %}

In your templates, extend the base and set the navigation location:

{% extends "base.html" %}

{% block nav %}
{% nav "home" %}
{{ block.super }}
{% endblock %}

Hierarchical navigation

To create a sub-menu you can check against, simply dot-separate the item:

{% nav "about_menu.info" %}

This will be pass for both {% if nav.about_menu %} and {% if nav.about_menu.info %}.

Using a different context variable

By default, this tag creates a nav context variable. To use an alternate context variable name, call {% nav [item] for [var_name] %}:

{% block nav %}
{% nav "home" for sidenav %}
{{ block.super }}
{% endblock %}

Setting the text output by the nav variable

As shown in the initial example, you can set the text return value of the nav context variable. Use the format {% nav text [content] %}. For example:

{% nav text "active" %}
<ul>
<li class="{{ nav.home }}">Home</li>
<li class="{{ nav.contact }}">Contact</li>
</ul>

Alternately, you can use boolean comparison of the context variable rather than text value:

<section{% if nav.home %} class="wide"{% endif %}>

If using a different context variable name, use the format {% nav text [content] for [var_name] %}.

Comparison operations

The nav object supports comparison operations for more flexible navigation handling:

Exact matching with ==:

{% nav "products.phones" %}

{% if nav == "products.phones" %}
    {# True - exact match #}
{% endif %}

{% if nav == "products" %}
    {# False - not exact #}
{% endif %}

Special patterns with !:

{% nav "products.electronics" %}

{% if nav == "products!" %}
    {# True - matches any child of products #}
{% endif %}

{% if nav == "products!clearance" %}
    {# True - matches children except 'clearance' #}
{% endif %}

Component checking with in:

{% nav "products.electronics.phones" %}

{% if "products" in nav %}
    {# True - component exists #}
{% endif %}

{% if "electronics" in nav %}
    {# True - component exists #}
{% endif %}

{% if "tablets" in nav %}
    {# False - component doesn't exist #}
{% endif %}

These operations also work with sub-navigation:

{% nav "products.electronics.phones" %}

{% if nav.products == "electronics.phones" %}
    {# True #}
{% endif %}

{% if "electronics" in nav.products %}
    {# True #}
{% endif %}

Iteration

The nav object supports iteration over its active path components:

{% nav "products.electronics.phones" %}

{% for component in nav %}
    {{ component }}
    {# Outputs: products, electronics, phones #}
{% endfor %}

This also works with sub-navigation:

{% nav "products.electronics.phones" %}

{% for component in nav.products %}
    {{ component }}
    {# Outputs: electronics, phones #}
{% endfor %}

Metadata

Release files for django-navtag 3.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-navtag 3.3.1
File Size Uploaded
django_navtag-3.3.1.tar.gz 10.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-navtag 3.3.1
File Interpreter ABI Platform
django_navtag-3.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 18.9 kB

Release files / django_navtag-3.3.1.tar.gz

Download URL django_navtag-3.3.1.tar.gz
Size 10.1 kB
Tags Source
SHA-256 checksum
How to use checksums
33099889d5b7df29c6028901226df1e8631f31567df94355fcd472b6af891462
BLAKE2b-256 checksum
How to use checksums
21e47b3e28d0502ceae70f60581b931eb47409e45b9874b5b0a736246c37e56b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.2

Release files / django_navtag-3.3.1-py3-none-any.whl

Download URL django_navtag-3.3.1-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
36a2901658f035f013cfa65b49e925f7dec346dafafaa84a3999b206e106bac4
BLAKE2b-256 checksum
How to use checksums
3fc4b0247979cc1ce9732096ee04300c5b05d392eafe5d511b1c46d5f524ea47
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.2

Release history Release notifications | RSS feed

This release

3.3.1 This release

2 release files

3.3

2 release files

3.2

2 release files

3.1

2 release files

3.0

2 release files

2.1.3

2 release files

2.1.2

2 release files

2.1.1

1 release file

2.1

1 release file

2.0

1 release file

1.1

1 release file

1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page