Changing active class in bootstrap navbar not stay

2019-09-02 11:22发布

问题:

My navbar lives in a base.html template which is extended by all pages. When I click a link in the navbar the active class is added but right after when I travel to that page it reverts to its previous state when only the initial page was highlighted.

Here is my base.html template

{% load static %}
<!-- DOCTYPE html -->
<html>
<head>
    <title>Title</title>
  <!-- new bootstrap -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">



  <script src="https://use.fontawesome.com/a0c7be9623.js"></script>

  <!-- css -->
  <link rel='stylesheet' href='{% static "css/base.css" %}' />

</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light fixed-top">
  <a class="navbar-brand" href='{% url "home" %}'>Maximum Likelihood</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
    <div class="navbar-nav mr-auto">
      <a class="nav-item nav-link active" href='{% url "home" %}'>Info<span class="sr-only">(current)</span></a>
      <a class="nav-item nav-link" href='{% url "about" %}'>About</a>
      <a class="nav-item nav-link" href='{% url "stuff" %}'>Stuff</a>
    </div>
    <div class="navbar-nav">
      <a class="nav-item nav-link ml-auto" href='google.com'>Outside link</a>
    </div>
  </div>
</nav>


{% block content %}
{% endblock content %}

<br>


<!-- <!-- bootstrap's js stuff -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js" integrity="sha384-cs/chFZiN24E4KMATLdqdvsezGxaGsi4hLGOzlXwp5UZB1LY//20VyM2taTB4QvJ" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js" integrity="sha384-uefMccjFJAIv6A+rW+L4AHf99KvxDjWSu1z9VI8SKNVmz4sk7buKt/6v9KI65qnm" crossorigin="anonymous"></script>
<script  src='{% static "js/nav.js" %}'/></script>
</body>

Here is my nav.js

$(".nav-link").on("click", function(){
   $(".nav").find(".active").removeClass("active");
   $(this).addClass("active");
});

回答1:

Best approach is to use django block more info

base.html

<!-- codes here -->
 <div class="navbar-nav mr-auto">
      <a class="nav-item nav-link {% block nav_item_home %}{% endblock nav_item_home %}" href='{% url "home" %}'>Book Materials <span class="sr-only">(current)</span></a>
      <a class="nav-item nav-link {% block nav_item_about %}{% endblock nav_item_about %}" href='{% url "about" %}'>About</a>
      <a class="nav-item nav-link {% block nav_item_stuff %}{% endblock nav_item_stuff %}" href='{% url "stuff" %}'>Stuff</a>
  </div>

when you are in the inherited template

just add this in the top of your page after extends of course.
Let's say we are in the about page:

{% extends 'base.html' %}
{% block nav_item_about %}active{% endblock nav_item_about %} 


回答2:

For some reason Django blocks aren't properly extending 'active' for me. I copied verbatim, the only difference is I'm trying it with a dropdown.

So this works:

<li class="nav-item dropdown active">

But this doesn't:

<li class="nav-item dropdown {% block 'index_active' %}{% endblock %}">

An example of my index or about html:

{% extends "client_side_app/base.html" %}

{% block about_active %}
    active
{% endblock %}

What's odd about this is that I have content blocks right below this above code that have no problem injecting the html into the base.html.

Any idea as to why this is happening? :(

Thanks,

Dev