Django导航,使用url.patterns模式

2024-04-25 08:22:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我学习Python,做测试项目。你知道吗

大小写:我需要根据页面url为<li>使用class="active"。现在我使用django.core.context_processors.requestpath获取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>

所以,如果我打开主页-first<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')

问题:怎么做?由于语法错误,我不能在if-语句中使用{% url 'groups' %}。我需要一些建议。你知道吗


Tags: pathurlhomeifrequestlipresentationviews