@app.context_processor
def mycontext():
usern=session.get('user')
if usern:
return {'usern':usern}
else:
return {}
@app.route('/logout/')
def logout():
session.clear()
return redirect(url_for('base'))
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>主页 </title>
{% if usern %}
<li><a href="#">{{ usern }}</a></li>
<li><a href="{{ url_for('logout') }}">注销</a></li>
{% else %}
<li><a href="{{ url_for('login') }}">登录</a></li>
<li><a href="{{ url_for('register') }}">注册</a></li>
{% endif %}
</head>
<body>
</body>
</html>