停止特定模板的导航栏的继承

时间:2017-02-17 07:45:06

标签: python html django

我正在开发一个Django项目,该网站还有一个导航栏。因此,当一个用户登录时,我想更改导航栏。那么如何阻止导航栏从基础模板继承?

{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
    <title>Blog</title>
        <title>Eapp</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->

    <link rel="stylesheet" href="{% static 'css/blog.css' %}">
</head>
<body>
    <div class="container">
    <nav class="navbar navbar-default">
      <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Eapp</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
          <ul class="nav navbar-nav navbar-right">
            <li><a href="{% url 'eapp:registration' %}">Register</a></li>
          </ul>
          <ul class="nav navbar-nav navbar-right">
            <li><a href="{% url 'eapp:login' %}">Login</a></li>
          </ul>
        </div><!-- /.navbar-collapse -->
      </div><!-- /.container-fluid -->
    </nav>
        {% block content %}
        {% endblock content%}
    </div>
</body>
</html>

这是我的base.html看起来像,请告诉我新模板的外观?谢谢

1 个答案:

答案 0 :(得分:1)

你可以使用django模板标签,例如这是模板标签,显示如果用户通过身份验证,那么它将显示注销按钮,否则登录。同样,您可以在base.html中为导航栏执行此操作。

{% if request.user.is_authenticated %}
      <li><a href="/logout">Log Out</a></li>
      {% else %}
      <li><a href="/login">Log In</a></li>
      {% endif %}