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

    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'))
  • 相关阅读:
    npm配置国内源方法
    数据库—事务—隔离级别
    Mybatis—日志
    Mybatis—动态 SQL
    Mybatis—mapper.xml配置文件
    declare命令
    shell杂项
    流程控制语句
    第一篇博客
    Linux 命令[2]:mkdir
  • 原文地址:https://www.cnblogs.com/liminghui3/p/7889091.html
Copyright © 2011-2022 走看看