我的导航栏位于base.html模板中,该模板由所有页面扩展。 当我单击导航栏中的链接时,会添加活动类,但是当我前往该页面时,只有当初始页面突出显示时,它才会恢复到之前的状态。
这是我的base.html模板
{% 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>
这是我的nav.js
$(".nav-link").on("click", function(){
$(".nav").find(".active").removeClass("active");
$(this).addClass("active");
});
答案 0 :(得分:2)
最佳方法是使用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>
当你在继承模板
时只需在extends
之后将其添加到页面顶部
让我们说我们在关于页面:
{% extends 'base.html' %}
{% block nav_item_about %}active{% endblock nav_item_about %}
答案 1 :(得分:0)
由于某种原因,Django块无法正确地为我扩展“活动”状态。我逐字复制,唯一的区别是我正在使用下拉菜单进行尝试。
这可行:
<li class="nav-item dropdown active">
但这不是:
<li class="nav-item dropdown {% block 'index_active' %}{% endblock %}">
我的索引或有关html的示例:
{% extends "client_side_app/base.html" %}
{% block about_active %}
active
{% endblock %}
奇怪的是,我在上面的代码下面有内容块,将html注入base.html没问题。
为什么会这样? :(
谢谢
开发