登录之后更新导航


  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':username}
    else:
        return {}
<nav>
    <img id="myOnOff" onclick="mySwitch()"
         src="{{ url_for('static',filename='image/base.jpg') }}" width="30px">
    <a href="{{ url_for('base') }}">首页</a>
    <a href="{{ url_for('regist') }}">注册</a>
    <a href="{{ url_for('login') }}">登录</a>
    <a href="{{ url_for('wenda') }}">问答</a>
    {% if username %}
        <li style="float:right"><a href="#">{{ username }}</a></li>
        <li style="float:right"><a href="#">注销</a></li>
    {% else %}
        <li style="float:right"><a href="{{ url_for('login') }}">登陆</a></li>
        <li style="float:right"><a href="{{ url_for('regist') }}">注册</a></li>
    {% endif %}
</nav>
@app.route('/logout/')
def logout():
    session.clear()
    return redirect(url_for('base'))
posted @ 2017-11-24 09:57  003刘淑千  阅读(178)  评论(0编辑  收藏  举报