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'))
  • 相关阅读:
    设计模式之——浅谈strategy模式(策略模式)
    设计模式之——bridge模式
    验证ip地址
    查询sqlserver数据库表的记录数
    iis网站部署常见错误
    asp.net 向后台提交 html 代码段 包括 <> 标签
    jquery花式图片库——jqFancyTransitions
    为sqlserver数据库添加专用用户名
    sqlserver 收缩数据库/文件
    你使用的ie版本过低请。。。
  • 原文地址:https://www.cnblogs.com/laidaili/p/7891948.html
Copyright © 2011-2022 走看看