A
A
Alexander Lebedev2015-09-03 21:39:02
Django
Alexander Lebedev, 2015-09-03 21:39:02

How to customize navigation in Django via url.patterns?

Good evening.
I'm learning Python, doing a test project.
Task: to make sure that for is assigned based on the page address. I implement via to pull the address from . Here's what it looks like now:class="active"<li>django.core.context_processors.requestpath

<li role="presentation" {% if request.path == '/' %}class="active"{% endif %}><a href="{% url 'home' %}">Главная</a></li>
<li role="presentation" {% if '/groups' in request.path %}class="active"{% endif %}><a href="{% url 'groups' %}">Группы</a></li>

That is, if the root - then the first li is active, and if the link contains "/groups", then the second one. This option works, but is categorically not flexible. I want to validate a link through configured 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 substitute in the -condition, because the syntax is broken and I don't know the correct syntax. I would be grateful for any advice :) {% url 'groups' %}if

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sim3x, 2015-09-03
@sortarage

There are three levels for solving such problems
0. Model. in the method you do the whole kitchen - the result is in the form of a dict. The downside is that it’s not always convenient to receive request
1. View, the same thing, only request is convenient to receive
2. Template, a template tag is written and magic happens inside it
In the test, you better do everything in the view in a separate function

I
IvanOne, 2015-09-07
@IvanOne

If the menu is simple without problems and there are not so many elements in it, then I usually reserve a block for each element and then in the right template I just write a class in this block, not a lot of dirty implementation but simple, for example:

<li class="{% block home %}{% endblock %} treeview">
           <a href="/">
                <i class="fa fa-rocket"></i>
                  <span>Главная</span>
            </a>
 </li>

This is in the base template , but in the child one: developers believe that this task is more related to the frontend, and should be solved on the client side.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question