user123892
user123892

Reputation: 1289

How to set a navbar link "active" for multiple url with Django tags

I have the following code

navbar.html:

{% url 'news:list' as news_url %}
{% url 'news:list-detail' as news_url_detail %}
[..]    
<ul class="navbar-nav">
 [..]        
 <li class="nav-item ">
   <a class="nav-link {% if request.path == news_url %}active{% endif %}" href="{{ news_url }}">News<span class="sr-only">(current)</span>
   </a>
 </li>
</ul>

My goal is to set as active the navbar link "News" even if the current url is not the "News" one (news_url)

<a class="nav-link {% if request.path == news_url or request.path == news_url_detail %}active{% endif %}" href="{{ news_url }}">News<span class="sr-only">(current)</span>

How can I do that?

Thank you for any help you can provide.

EDIT 1 /core/app/urls.py

urlpatterns = [
    [..]
    url(r'^newsandeventi/', include("newsandeventi.urls", namespace='newsandeventi')),
    [..]
]

/newsandeventi/urls.py

from django.conf.urls import url, include
from newsandeventi.views import ArchiveView, CategoryListView, SearchView, EntryDetailView
# from blog.views import EntryDetailView, CategoryListView, ArchiveView, SearchView, SearchResultView # noqa


urlpatterns = [
    url(r'^$', ArchiveView.as_view(), name='list'),
    url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<slug>[-\w]+)/$', EntryDetailView.as_view(), name='list-detail'),    
    url(r'^categoria/(?P<tag>[-\w .]+)/$', CategoryListView.as_view(), name='category-list'),
]

EDIT 2: /newsandeventi/views.py

class EntryDetailView(DetailView):
    model = Entry
    paginate_by = 1

    def get_queryset(self):
        return Entry.objects.published()

    def get_context_data(self, **kwargs):
        context = super(EntryDetailView, self).get_context_data(**kwargs)
        context['base_url'] = self.request.build_absolute_uri("/").rstrip("/")
        context['alltags'] = Tag.objects.all()
        context['fiveposts'] = Entry.objects.order_by('id')[:5]
        entries= Entry.objects.all()
        context['last_featured'] = entries.filter(featured=True).last()
        return context

The issue might rise from the class-based view EntryDetailView, when I call newsandeventi:list-detail, I'm not passing through the arguments year, month, day, slug.

If this is the issue, How can I solve it?

Upvotes: 1

Views: 507

Answers (1)

Will Keeling
Will Keeling

Reputation: 23024

The problem is that this line:

{% url 'news:list-detail' as news_url_detail %}

will silently fail because you're not passing the year/month/day/slug parameters required by the URL. The {% url ... as var %} syntax does not cause an error if the view lookup fails.

A better approach to identify whether the rendered page is an EntryDetailView may be to set a flag in the context data, and then test for the existence of that flag.

For example:

class EntryDetailView(DetailView):
    ...

    def get_context_data(self, **kwargs):
        ...
        context['is_entry'] = True  # Set a flag
        return context

And then in the template:

<a class="nav-link {% if request.path == news_url or is_entry %}active{% endif %}" href="{{ news_url }}">News<span class="sr-only">(current)</span>

Upvotes: 1

Related Questions