[英]Navigation in Django, using url.patterns
I learn Python, doing test project. 我学习Python,做测试项目。
Case: I need to use class="active"
for <li>
based on page url. 案例:我需要基于页面URL为
<li>
使用class="active"
。 Now I use django.core.context_processors.request
to take url from path
. 现在我使用
django.core.context_processors.request
从path
获取URL。
How it looks now: 现在的样子:
<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>
So, if I open main page - first <li>
is active, if url contains "/groups"
- secon <li>
is active. 因此,如果我打开主页-第一个
<li>
是活动的,如果url包含"/groups"
-secon <li>
是活动的。 And it works, but I want to do it more complex, comparing url with url.patterns
in urls.py
: 它可以工作,但我想做得更复杂,将url与
urls.py
url.patterns
进行比较:
url(r'^$', 'students.views.students_list', name='home'),
url(r'^groups/$', 'students.views.groups_list', name='groups')
Question: how to do it? 问题:怎么做? I can't just use
{% url 'groups' %}
in if
-statement because of syntax error. 由于语法错误,我不能仅在
if
-statement中使用{% url 'groups' %}
url'groups {% url 'groups' %}
。 Need some advice. 需要一些建议。
You can assign the result of url tag to the variable. 您可以将url标记的结果分配给该变量。
{% url 'groups' as groups_url %}
# use groups_url variable somewhere
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.