登录之后更新导航

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

@app.route('/logout/')
def logout():
    session.clear()
    return  redirect(url_for('jianshu'))
{% if username %}
    <a href="#">{{ username }}</a>
    <a href="{{ url_for('logout') }}">注销</a>
    {% else %}
    <a href="{{ url_for('login') }}">登陆</a>
    <a href="{{ url_for('enroll') }}">注册</a>
    {% endif %}

原文地址:https://www.cnblogs.com/lintingting/p/7889074.html