如何在Django导航栏下拉列表中显示活动链接? [英] How do i show an active link in a django navigation bar dropdown list?

查看:48
本文介绍了如何在Django导航栏下拉列表中显示活动链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个导航栏菜单,其中包含链接列表,我想在用户位于页面上时显示活动链接,到目前为止,我已经设法通过没有此类下拉菜单的链接来做到这一点.

I have a navbar menu with a list of links which i want to show the active link when the user is on the page, so far i have managed to do this with links that dont have dropdowns like this.

但是我似乎无法正确地使用下拉链接,如果用户在下拉链接的页面上,导航栏上的父链接就会突出显示,如下所示

But I cannot seem to get it right with the dropdown links in such a way that if the user is on a page on the dropdown link the parent link on the navbar gets highlighted.like shown below

任何帮助将不胜感激.

推荐答案

如果您使用以下名称定义网址:

If you define your URLs with names like this:

url('', 'home_view', name='home'),
url('posts/', 'posts_view', name='blog'),
url('contact/', 'contact_view', name='contact'),

您可以在模板中使用以下名称来使if语句起作用:

You can use these names in the template to make the if statements work:

{% with request.resolver_match.url_name as url_name %}
    <ul id="menu">
        <li class="{% if url_name == 'home' %}active{% endif %}">Home</li>
        <li class="{% if url_name == 'blog' %}active{% endif %}">Posts</li>
        <li class="{% if url_name == 'contact' %}active{% endif %}">Contact</li>
    </ul>
{% endwith %}

这使您免于URL路径重复的麻烦.

This saves you from problems with duplication in url paths.

这篇关于如何在Django导航栏下拉列表中显示活动链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆