python - 如何在 django 导航栏下拉列表中显示事件链接?

标签 python django python-2.7 python-3.x django-templates

我有一个导航栏菜单,其中包含一个链接列表,我想在用户位于页面上时显示事件链接,到目前为止,我已经成功地使用没有这样的下拉菜单的链接来做到这一点。

enter image description here

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

enter image description here

任何帮助将不胜感激。

最佳答案

如果您使用如下名称定义 URL:

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

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

{% 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 路径重复的问题。

关于python - 如何在 django 导航栏下拉列表中显示事件链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46617375/

相关文章:

Python websocket,无法接收消息

python - 如何在条形图中从一列列表中绘制词频

django - 如何修复/admin/login/处的编程错误(1146, "Table 'torquedb.showroom_customuser'不存在”)

django - 如何在 Django 中向 User.groups.through 添加一个字段?

python / split : How to find and select an option on a site?

python - 将列表项放入队列中

python - 需要帮助使用 Ctypes 加载共享库

python - 如何在 Django 中将 2 个模型字段合并在一起?

python - 在使用 mpi4py 的并行环境中第一次迭代后,在 for 循环中使用 Popen 的系统调用失败

python - 程序退出时如何删除文件?