登录之后更新导航

  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 {}
{% if username %}
        <a href="#">{{ username }}</a>
        <a href="{{ url_for('zhuxiao') }}">注销</a>
    {% else %}
        <a href="{{ url_for('denglu') }}">登录</a>
        <a href="{{ url_for('zhuce') }}">注册</a>
    {% endif %}
@app.route('/zhuxiao')
def zhuxiao():
    session.clear()
    return redirect(url_for('daohang'))
原文地址:https://www.cnblogs.com/xiaojiaqi/p/7889064.html