Package: django-simple-nav
django-simple-navdjango-simple-navis a Python/Django application designed to simplify the integration of navigation and menu bars in your Django projects. With a straightforward API and customizable options, you can easily add and manage navigational elements in your web applications. It is designed to be simple to start with, but flexible enough to handle complex navigation structures while maintaining that same simplicity.RequirementsPython 3.8, 3.9, 3.10, 3.11, 3.12Django 3.2, 4.2, 5.0Getting StartedInstall the package from PyPI.python-mpipinstalldjango-simple-navAdddjango_simple_navtoINSTALLED_APPS.After installation, adddjango_simple_navto yourINSTALLED_APPSin your Django settings:INSTALLED_APPS=[...,"django_simple_nav",...,]UsageCreate a navigation definition.Define your navigation structure in a Python file. This file can be located anywhere in your Django project, provided it's importable. You can also split the navigations across multiple files if desired.A good starting point is to create a singlenav.pyornavigation.pyfile in your Django project's main configuration directory (where yoursettings.pyfile is located).django-simple-navprovides three classes to help you define your navigation structure:Nav: The main container for a navigation structure. It has two required attributes:template_name: The name of the template to render the navigation structure.items: A list ofNavItemorNavGroupobjects that represent the navigation structure.NavGroup: A container for a group ofNavItemorNavGroupobjects. It has two required and three optional attributes:title: The title of the group.items: A list ofNavItemorNavGroupobjects that represent the structure of the group.url(optional): The URL of the group. If not provided, the group will not be a link but just a container for the items.permissions(optional): A list of permissions that control the visibility of the group. These permissions can beUserattributes (e.g.is_authenticated,is_staff,is_superuser) or Django permissions (e.g.myapp.django_perm).extra_context(optional): A dictionary of additional context to pass to the template when rendering the navigation.NavItem: A single navigation item. It has two required and three optional attributes:title: The title of the item.url: The URL of the item. This can be a URL string (e.g.https://example.com/about/or/about/) or a Django URL name (e.g.about-view).permissions(optional): A list of permissions that control the visibility of the item. These permissions can beUserattributes (e.g.is_authenticated,is_staff,is_superuser) or Django permissions (e.g.myapp.django_perm).extra_context(optional): A dictionary of additional context to pass to the template when rendering the navigation.Here's an example configuration:# config/nav.pyfromdjango_simple_nav.navimportNavfromdjango_simple_nav.navimportNavGroupfromdjango_simple_nav.navimportNavItemclassMainNav(Nav):template_name="main_nav.html"items=[NavItem(title="Relative URL",url="/relative-url"),NavItem(title="Absolute URL",url="https://example.com/absolute-url"),NavItem(title="Internal Django URL by Name",url="fake-view"),NavGroup(title="Group",url="/group",items=[NavItem(title="Relative URL",url="/relative-url"),NavItem(title="Absolute URL",url="https://example.com/absolute-url"),NavItem(title="Internal Django URL by Name",url="fake-view"),],),NavGroup(title="Container Group",items=[NavItem(title="Item",url="#"),],),NavItem(title="is_authenticated Item",url="#",permissions=["is_authenticated"]),NavItem(title="is_staff Item",url="#",permissions=["is_staff"]),NavItem(title="is_superuser Item",url="#",permissions=["is_superuser"]),NavItem(title="myapp.django_perm Item",url="#",permissions=["myapp.django_perm"]),NavGroup(title="Group with Extra Context",items=[NavItem(title="Item with Extra Context",url="#",extra_context={"foo":"bar"},),],extra_context={"baz":"qux"},),]Create a template for the navigation.Create a template to render the navigation structure. This is just a standard Django template so you can use any Django template features you like.The template will be passed anitemsvariable in the context representing the structure of the navigation, containing theNavItemandNavGroupobjects defined in your navigation.Any items with permissions attached will automatically filtered out before rendering the template based on the request user's permissions, so you don't need to worry about that in your template.Items with extra context will have that context passed to the template when rendering the navigation, which you can access either directly or through theitem.extra_contextattribute.For example, given the above exampleMainNav, you could create amain_nav.htmltemplate:<!-- main_nav.html --><ul>{%foriteminitems%}<li><ahref="{{item.url}}"{%ifitem.active%}class="active"{%endif%}{%ifitem.baz%}data-baz="{{item.baz}}"{%endif%}>{{item.title}}</a>{%ifitem.items%}<ul>{%forsubiteminitem.items%}<li><ahref="{{subitem.url}}"{%ifsubitem.active%}class="active"{%endif%}{%ifitem.extra_context.foo%}data-foo="{{item.extra_context.foo}}"{%endif%}>{{subitem.title}}</a></li>{%endfor%}</ul>{%endif%}</li>{%endfor%}</ul>Integrate navigation in templates.:Use thedjango_simple_navtemplate tag in your Django templates where you want to display the navigation.For example:<!-- base.html -->{%loaddjango_simple_nav%}{%blocknavigation%}<nav>{%django_simple_nav"path.to.MainNav"%}</nav>{%endblocknavigation%}The template tag can either take a string representing the import path to your navigation definition or an instance of your navigation class:# example_app/views.pyfromconfig.navimportMainNavdefexample_view(request):returnrender(request,"example_app/example_template.html",{"nav":MainNav()})<!-- example_app/example_template.html -->{%extends"base.html"%}{%loaddjango_simple_nav%}{%blocknavigation%}<nav>{%django_simple_navnav%}</nav>{%endblocknavigation%}Additionally, the template tag can take a second argument to specify the template to use for rendering the navigation. This is useful if you want to use the same navigation structure in multiple places but render it differently.<!-- base.html -->{%loaddjango_simple_nav%}<footer>{%django_simple_nav"path.to.MainNav""footer_nav.html"%}</footer>After configuring your navigation, you can use it across your Django project by calling thedjango_simple_navtemplate tag in your templates. This tag dynamically renders navigation based on your defined structure, ensuring a consistent and flexible navigation experience throughout your application.DocumentationPlease refer to thedocumentationfor more information.Licensedjango-simple-navis licensed under the MIT license. See theLICENSEfile for more information.