Package: django-activeurl
Easy-to-use active URL highlighting for DjangoFeaturesautomatic highlighting of currently active<a>tags via CSS classautomatic highlighting of parent<a>tags for menusremoves boring / hardcoded stuff from your life!href=”#” never causes highlighting for compatibility with techniques such as
bootstrap nav.UsageAfter loading the template library via{%loadactiveurl%}the following code snippet will be rendered like this ifrequest.full_path()starts with/some_page/:{%activeurl%}<ul><li><!-- this <li> will render as <li class="active"> --><ahref="/some_page/">some_page</a></li><li><ahref="/another_page/">another_page</a></li></ul>{%endactiveurl%}Note:The content of{% activeurl%}…{%endactiveurl %}must have valid root tag (i.e.<ul>or<div>, etc) – otherwise an exception will be raised.InstallationPython 2.7+, 3.4+ are supported.Install thestableversion usingpip:pip install django-activeurlor install thein-developmentversion usingpip:pip install -e git+git://github.com/hellysmile/django-activeurl#egg=django_activeurlIn yoursettings.pyadd the following:INSTALLED_APPS=(...'django_activeurl',...)TEMPLATE_CONTEXT_PROCESSORS=(...'django.core.context_processors.request',...)Thelxmllibrary is required and thus additional libraries need to be installed to build it:Ubuntu:sudo apt-get install libxml2 libxml2-dev libxslt-dev build-essential python-dev
sudo ldconfigFedora:sudo yum groupinstall 'Development Tools'
sudo yum install libxslt-devel libxml2 libxml2-devel python-devel
sudo ldconfigMacOS X:brew install libxml2 libxslt
sudo update_dyld_shared_cache -forceWindows:
A pre-builtlxmlbinary can be foundhereClouds:
There’s a 99.99% chance thatlxmlwill build out of the box.Optionsmenu =”yes|no” (default: “yes”)Should hierarchical menus be supported? There are two different ways to declare anactivestatus:thestarts-withlogic toggles the active state ifrequest.get_full_path()starts with the
contents of the<a href=attribute.theequalslogic toggles the active state ifrequest.get_full_path()is identical to the
contents of the<a href=attribute.You might want to usestarts-with logicin hierarchical menus/submenus to not only highlight the current position but also every parent position. So, withrequest.get_full_path()being/menu/submenuthe following snippet will render accordingly:{%activeurlmenu="yes"parent_tag="div"%}<div><div><!-- This will render as <div class="active"> --><ahref="/menu/">menu</a><div><!-- This will also render as <div class="active"> --><ahref="/menu/submenu/">submenu</a></div></div></div>{%endactiveurl%}Theequalslogic works best for non-hierarchical menus where only those items should be highlighted whosehref-attribute perfectly matchrequest.get_full_path():{%activeurlmenu="no"parent_tag="div"%}<div><div><ahref="/menu/">menu</a></div><div><ahref="/menu/submenu/">submenu</a></div></div>{%endactiveurl%}ignore_params =”yes|no” (default: “no”)ignore_paramswill ignore GET parameters of URLs, e.g./accounts/login/will match/accounts/login/?next=/accounts/signup/.parent_tag =”div|li|self|…” (default: “li”)parent_tagdefines that a parent element – and not the<a>tag itself – should be declaredactivewhen there’s a match in URLs. When you need to change the CSS class of the<a>tag, just enter “self”.css_class =”<string>” (default: “active”)Defines what CSS class to add to an active element.ConfigurationThe default options can be set insettings.pyas well:ACTIVE_URL_KWARGS={'css_class':'active','parent_tag':'li','menu':'yes','ignore_params':'no'}ACTIVE_URL_CACHE=TrueACTIVE_URL_CACHE_TIMEOUT=60*60*24# 1 dayACTIVE_URL_CACHE_PREFIX='django_activeurl'By defaultdjango-activeurlwill try to retrieve a previously rendered HTML node from Django’s caching backend before active URLs are looked for and a new HTML tree is built. You can disable the cache withACTIVE_URL_CACHE = False.In addition,ACTIVE_URL_CACHE_TIMEOUTcan be used to define a timeout for keys to expire. The default value is one day.The last configuration option isACTIVE_URL_CACHE_PREFIX(which isdjango_activeurlby default) and defines which name to use in Django’s caching backend.Testspip install tox
toxJinja2VanillaJinja2configuration:fromjinja2importEnvironmentfromdjango_activeurl.ext.django_jinjaimportActiveUrlenv=Environment(extensions=[ActiveUrl])Options can be omitted:{%activeurlcss_class="active",menu="yes",parent_tag="li",ignore_params="no"%}<ul>
        <li>
            <a href="/page/">page</a>
        </li>
        <li>
            <a href="/other_page/">other_page</a>
        </li>
    </ul>{%endactiveurl%}If you’re usingdjango-jinjayou need to load theActiveUrlinsettings.py.Django 1.8+ Jinja2 environment loader example can be found intests.BackgroundFor building the HTML element treedjango-activeurluseslxml, which is one of the best HTML parsing
tools around. More info and benchmarks can be found athabrahabr.ru(in russian). Note that there’s no
content rebuilding inside the template tag when no active URLs are found, so
there’s no impact on performance.