Reputation: 1673
I learn Python, doing test project.
Case: I need to use class="active"
for <li>
based on page url. Now I use django.core.context_processors.request
to take url from path
.
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. And it works, but I want to do it more complex, comparing url with url.patterns
in urls.py
:
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. Need some advice.
Upvotes: 0
Views: 114
Reputation: 376
You can assign the result of url tag to the variable.
{% url 'groups' as groups_url %}
# use groups_url variable somewhere
Upvotes: 1