zoukankan      html  css  js  c++  java
  • 登录之后更新导航

    1、用上下文处理器app_context_processor定义函数

    获取session中保存的值

    返回字典

    2、在父模板中更新导航,插入登录状态判断代码。、

    注意用{% ... %}表示指令。

    {{ }}表示变量

    3、完成注销功能。

    清除session

    跳转

    @app.context_processor
    def mycontext():
        usern=session.get('user')
        if usern:
            return{'username':usern}
        else:
            return{}
     <p>{{ username }}一三</p>
    {% if username %}
                    <a href="#">
                    <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-log-in"></span>{{ username }}
                    </button>
                    </a>
                    <a href="{{ url_for('logout') }}">
                        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-user"></span> 注销                   </button>
                    </a>
                    {% else %}
                    <a href="{{ url_for('login') }}">
                        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-log-in"></span>登录
                        </button>
                    </a>
                    <a href="{{ url_for('regist') }}">
                        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-user"></span> 注册
                        </button>
                    </a>
                    {%  endif %}<a href="#">
                    <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-log-in"></span>{{ username }}
                    </button>
                    </a>
                    <a href="{{ url_for('logout') }}">
                        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-user"></span> 注销                   </button>
                    </a>
                    {% else %}
                    <a href="{{ url_for('login') }}">
                        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-log-in"></span>登录
                        </button>
                    </a>
                    <a href="{{ url_for('regist') }}">
                        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-user"></span> 注册
                        </button>
                    </a>
                    {%  endif %}
    @app.route('/logout')
    def logout():
        session.clear()
        return redirect(url_for('base'))
  • 相关阅读:
    HDU2206:IP的计算
    HDU 2054 A == B ?A
    怎样确定循环节
    Python和Java编程题(二)
    Python和Java编程题(一)
    Java中的静态变量、静态方法问题
    快速排序的Java和python实现,亲测实际可用
    Java和Python分别实现直接选择排序
    Python和Java分别实现冒泡排序
    JavaScript(第十六天)【BOM基础】
  • 原文地址:https://www.cnblogs.com/laidaili/p/7891948.html
Copyright © 2011-2022 走看看