Answer the question
In order to leave comments, you need to log in
How to check Request.path in Django?
Here is the urls.py code
url(r'^category/(?P<cat_id>\d+)$', 'adst.views.alboms', name='Albom'),
{% for cat in categor %}
<li {% if cat.id_cat in request.path %}class="active"{% endif %} >
<a href="{% url "Albom" cat.id_cat %}">{{ cat.Name_cat }}</a>
<span class="sr-only">(current)</span>
</li>
{% endfor %}
Answer the question
In order to leave comments, you need to log in
And exactly id_cat? By default, dzhang does something completely different. Typically, models have a primary key called id . Or, if a non-standard name is used, it can be referred to via the pk alias .
Thus, if I correctly understood that we are talking about enumeration of the queriset, then the condition should look something like this:
Not the most, I must say, a good solution. It would be more correct, for example, to implement the get_absolute_url() method on the model and compare request.path with it in a loop.
id_alb = models.AutoField(primary_key=True)
- bad practice. For what purpose did you change the standard id to exactly the same, but named id_alb?
In general, this is not the only remark to your code. Your URL is called with a capital letter and, in general, is called Russian transliteration (very bad practice). {% url 'Albom' cat.id_cat %} should be replaced with {{ cat.get_absolute_url }} and, accordingly, add the Cat model method to models.py:
# В начале:
from django.core.urlresolvers import reverse
# Метод модели:
def get_absolute_url(self):
return reverse('name_of_your_detail_view_for_cutties', args=[self.pk])
$(function(){
function stripTrailingSlash(str) {
if(str.substr(-1) == '/') {
return str.substr(0, str.length - 1);
}
return str;
}
var url = window.location.pathname;
var activePage = stripTrailingSlash(url);
$('.nav li a').each(function(){
var currentPage = stripTrailingSlash($(this).attr('href'));
if (activePage == currentPage) {
$(this).parent().addClass('active');
}
});
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question