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

浙公网安备 33010602011771号