- 用上下文处理器app_context_processor定义函数
- 获取session中保存的值
- 返回字典
- 在父模板中更新导航,插入登录状态判断代码。、
- 注意用{% ... %}表示指令。
- {{ }}表示变量
- 完成注销功能。
- 清除session
- 跳转
1 @app.context_processor
2 def mycontext():
3 usern = session.get('user')
4 if usern:
5 return {'username':usern}
6 else:
7 return {}
1 <ul class="nav navbar-nav navbar-right">
2 {% if username %}
3 <li><a href="#">{{ username }}</a></li>
4 <li><a href="{{ url_for('logout') }}">注销</a></li>
5 {% else %}
6 <li><a href="{{ url_for('login') }}">登录</a></li>
7 <li><a href="{{ url_for('regist') }}">注册</a></li>
8 {% endif %}
9 </ul>
1 @app.route('/logout')
2 def logout():
3 session.clear()
4 return redirect(url_for('index'))