python - About looping lists in django templates
欧阳克
欧阳克 2017-07-05 10:34:48
0
2
981

There is a navigation bar, and I want to add a class = 'on' attribute when this navigation is activated
If you click on the second navigation item, add this class to the second item

{% for nav in NAV %}
<li>nav</li>
{% end for %}

for example

<li class="on">第一项</li>
<li>第二项</li>
<li>第三项</li>

How to implement this?

欧阳克
欧阳克

温故而知新,可以为师矣。 博客:www.ouyangke.com

reply all(2)
淡淡烟草味
#后端
navs = [
    {'name': '菜单1', 'url': 'url1'},
    {'name': '菜单2', 'url': 'url2'}
]

for nav in navs:
    nav['class'] = 'on' if nav['url'] == request.path else None
    
#前端
{% for nav in navs %}
<li class={{ nva.class }}>{{ nva.name }}</li>
{% end for %}
ringa_lee

You have nothing to do with django.
This is a problem with the front-end page. For example:
You load all the navigation to the front-end page at once, and then you need to open the current menu based on clicks, and then turn off the effects of other menus, right?
If I understand correctly, this is a front-end problem

1. Bootstrap or UIKit all support this effect;

2. Write the navigation style yourself, for example, name it active, and then give you the clicked object in jQuery $(this).addClass('active'), this is an idea;

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template