我学习Python,做测试项目。
案例:我需要使用class="active"来实现基于页面url的<li>。现在我使用django.core.context_processors.request从path获取url。
现在的情况:
<li role="presentation" {% if request.path == '/' %}class="active"{% endif %}><a href="{% url 'home' %}">Home</a></li>
<li role="presentation" {% if '/groups' in request.path %}class="active"{% endif %}><a href="{% url 'groups' %}">Groups</a></li>所以,如果我打开主页-第一个<li>是活动的,如果url包含"/groups" - secon <li>是活动的.它可以工作,但我想做的更复杂,比较url和url.patterns中的urls.py
url(r'^$', 'students.views.students_list', name='home'),
url(r'^groups/$', 'students.views.groups_list', name='groups')问题:怎么做?由于语法错误,我不能只在{% url 'groups' %}中使用if-statement。需要一些建议。
发布于 2015-09-03 20:18:58
您可以将url标记的结果赋值给变量。
{% url 'groups' as groups_url %}
# use groups_url variable somewherehttps://stackoverflow.com/questions/32383260
复制相似问题