登录之后更新导航

  1. 用上下文处理器app_context_processor定义函数
    1. 获取session中保存的值
    2. 返回字典
@app.context_processor
def mycontext():
    username = session.get('user')
    if username:
        return {'username': username}
    else:
        return {}
  1. 在父模板中更新导航,插入登录状态判断代码。、
    1. 注意用{% ... %}表示指令。
    2. {{ }}表示变量
     {% if username %}
                        <a href="" class="right">注销</a>
                        <a href="" class="right">{{ username }}</a>
                    {% else %}
                        <a href="{{ url_for('login')}}" class="right" >登录</a>
                        <a href="{{ url_for('register')}}" class="right" >注册</a>
                    {% endif %}
  1. 完成注销功能。
    1. 清除session
    2. 跳转
@app.route('/loginout')
def loginout():
    session.clear()
    return redirect(url_for('index'))

原文地址:https://www.cnblogs.com/mavenlon/p/7890114.html