How to use active class of bootstrap based on page in django ?

Submitted 4 months, 3 weeks ago
Ticket #422
Views 75
Language Django
Priority Medium
Status Closed

Hello everyone ,
I am having problem in using active class in django based on page opened when it is inside of inscription_detail page. Can anyone help me?

Note: below code shows active in inscription page but dont show in when we go inside the inscription_detail page. Here I want the page to be like this when we go to

inscriptions/

then in navbar inscription must be active and when we go to this page clicking the list in inscription

inscriptions/<int:year>/<int:month>/<int:day>/<slug:inscription>/

then also inscription in navbar must be active .

Here my navbar.html:

{% url 'core:inscription_list' as inscription_list%}
{% url 'core:inscription_detail'  as inscription_detail%}


<li class="nav-item  ">
   <a class="nav-link {% if request.path == inscription_list %} active {% endif %} {% if request.path == inscription_detail %} active {% endif %}" href="{% url 'core:inscription_list' %}">inscriptions</a>
</li>

Here is urls.py:

    path('inscriptions/', views.InscriptionListView.as_view(), name='inscription_list'),
    path('inscriptions/<int:year>/<int:month>/<int:day>/<slug:inscription>/', views.inscription_detail,
         name='inscription_detail'),

and views.py:

class InscriptionListView(ListView):
    queryset = Inscription.published.all()
    context_object_name = 'inscriptions'
    template_name = 'core/inscription/inscriptions.html'

def inscription_detail(request, year, month, day, inscription):
    inscription = get_object_or_404(Inscription, slug=inscription,
                                    status='published',
                                    publish__year=year,
                                    publish__month=month,
                                    publish__day=day)
    return render(request, 'core/inscription/inscriptions-detail-1.html', {'inscription': inscription})

Submitted on Apr 27, 21

when I checked by trying to print context value in template it did not show the value of inscription_detail . can i anyone tell me how to write view for inscription_detail ? - Shrestha7 4 months, 3 weeks ago
add a comment

1 Answer

Verified

views.py

def inscription_detail(request, year, month, day, inscription):
    inscription = get_object_or_404(Inscription, slug=inscription,
                                    status='published',
                                    publish__year=year,
                                    publish__month=month,
                                    publish__day=day)
    return render(request, 'core/inscription/inscriptions-detail-1.html', {'inscription': inscription,'inscription_detail':'active'})

navbar.html

{% url 'core:inscription_list' as inscription_list%}
          <li class="nav-item  ">
            <a class="nav-link {% if request.path == inscription_list %} active {% endif %} {{inscription_detail}}" href="{% url 'core:inscription_list' %}">inscriptions</a>
          </li>

Submitted 4 months, 3 weeks ago


Wanna Post Ad?
Reach a large group of audience by posting an ad about your business here. For more details
Drop us a Message