登录之后更新导航

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

  1. 获取session中保存的值
  2. 返回字典
@app.context_processor
def mycontext():
    username=session.get('user')
    if username:
        return {'username':username}
    else:
        return{}

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

  1. 注意用{% ... %}表示指令。
  2. {{ }}表示变量
{% if username %}
        <li><a href="#">{{ username }}</a></li>
        <a href="{{ url_for('logout') }}">注销</a>
    {% else %}
        <li><a href="{{ url_for('login')}}">登录</a></li>
        <li><a href="{{ url_for('regist')}}">注册</a></li>
    {% endif %}

3.完成注销功能。

  1. 清除session
  2. 跳转
@app.route('/logout')
def logout():
    session.clear()
    return  redirect(url_for('base'))

 

posted @ 2017-11-24 16:04  004熊锋阳  阅读(106)  评论(0编辑  收藏  举报