1. 用上下文处理器app_context_processor定义函数
    1. 获取session中保存的值
    2. 返回字典
      @app.context_processor
      def mycontext():
          usern =session.get('user')
          if usern:
              return {'username':usern}
          else:
              return {}

       

       

  2. 在父模板中更新导航,插入登录状态判断代码。、
    1. 注意用{% ... %}表示指令。
    2. {{ }}表示变量
      {% if username %}
                  <li style="float:right"><a href="#" >注销</a></li>
                  <li style="float:right"><a href="#" >{{ username }}</a></li>
              {% else %}
                  <li style="float:right"><a href="{{ url_for('register') }}">注册</a></li>
                  <li style="float:right"><a href="{{ url_for('login') }}">登录</a></li>
              {% endif %}

       

       

  3. 完成注销功能。
    1. 清除session
      {% if username %}
                  <li style="float:right"><a href="{{ url_for('logout') }}" >注销</a></li>
                  <li style="float:right"><a href="#" >{{ username }}</a></li>
              {% else %}
                  <li style="float:right"><a href="{{ url_for('register') }}">注册</a></li>
                  <li style="float:right"><a href="{{ url_for('login') }}">登录</a></li>
              {% endif %}

       

    2. 跳转
      @app.route('/logout')
      def logout():
          session.clear()
          return redirect(url_for('index'))