Python学习自动化测试

动态自适应的NavBar - Django

2022-03-08  本文已影响0人  Kewings

首先在base.html里增加一个NavBar代码:

{% block nav %}
    <nav class="navbar navbar-expand-lg navbar-light bg-light">
      <a class="navbar-brand" href="#">Test</a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
    
      <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <ul class="navbar-nav mr-auto">
          <li {%if active_btn == 'db_btn' %} class="nav-item active" {% else %} class="nav-item" {%endif%}>
            <a class="nav-link" href="/">DB</a>
          </li>
          <li {%if active_btn == 'equip_btn' %} class="nav-item active" {% else %} class="nav-item" {%endif%}>
            <a class="nav-link" href="/equip/EquipStatus/">Equip</a>
          </li>
          <li {%if active_btn == 'plt_btn' %} class="nav-item dropdown active" {% else %} class="nav-item dropdown" {%endif%}>
            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
              Platform
            </a>
            <div class="dropdown-menu" aria-labelledby="navbarDropdown">
              <a class="dropdown-item" href="#">test1</a>
              <a class="dropdown-item" href="#">test2</a>
            </div>
          </li>
        </ul>
        {% if back_btn %}
        <a class="btn btn-outline-success my-2 my-sm-0" href={{back_url}}>返回</a>
        {% endif %}
        {% comment %} <form class="form-inline my-2 my-lg-0">
        <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
        <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
        </form> {% endcomment %}
      </div>
    </nav>
{% endblock%}

在views.py中返回字典中增加状态表征:

def testNavBar(request):
    if request.method == 'GET':
        res = {
            'active_btn': 'equip_btn',
            'back_btn': True,
            'back_url': '/equip/EquipStatus/',
        }
        return render(request, 'base.html', res)

这样就可以为页面的NavBar聚焦Active的按钮是哪个,以及如果需要返回按钮显示的话,back_btn=True即可,同时传递back_url给返回按钮

上一篇 下一篇

猜你喜欢

热点阅读