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