登录之后更新导航(2017.11.24)
Posted 赖黛俐
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了登录之后更新导航(2017.11.24)相关的知识,希望对你有一定的参考价值。
1、用上下文处理器app_context_processor定义函数
获取session中保存的值
返回字典
处理器:
@app.context_processor def mycontext(): usern=session.get(\'user\') if usern: return{\'username\':usern} else: return{}
首页:
</nav> {% block main %} <p>{{ username }}呆梨</p> {% endblock %} <a href="https://baike.so.com/create/edit/?eid=4909310&sid=5127865"> <img src="https://p1.ssl.qhmsg.com/t01d08b6f07a9225737.png" style="width: 200px"></a> <h4 class="textblue">代表作品:</h4> <div class="recommend"> <div class="img"> <h5><a herf="https://baike.baidu.com/item/%E6%9D%8E%E6%98%93%E5%B3%B0/3908480?fr=aladdin"></a></h5> <img src="https://gss1.bdstatic.com/9vo3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=ccb6b48247166d222c7a1dc6274a6292/48540923dd54564e0b15d770b7de9c82d1584f02.jpg"></a> <div class="desc"><a href="https://baike.baidu.com/item/%E5%8F%A4%E5%89%91%E5%A5%87%E8%B0%AD/5016869">古剑奇谭</a> </div> </div> <div class="img"> <a herf="https://baike.baidu.com/item/%E6%9D%8E%E6%98%93%E5%B3%B0/3908480?fr=aladdin"> <img src="https://gss1.bdstatic.com/-vo3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike272%2C5%2C5%2C272%2C90/sign=1c3a441f8a025aafc73f76999a84c001/b21bb051f8198618fb53ed5b42ed2e738ad4e6dd.jpg"></a> <div class="desc"><a href="https://baike.baidu.com/item/%E9%BA%BB%E9%9B%80/17612774">麻雀</a></div> </div> <div class="img"> <a herf="https://baike.baidu.com/item/%E6%9D%8E%E6%98%93%E5%B3%B0/3908480?fr=aladdin"> <img src="https://gss3.bdstatic.com/7Po3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike150%2C5%2C5%2C150%2C50/sign=06534c58b0fd5266b3263446ca71fc4e/8326cffc1e178a823e993f13f003738da877e8d8.jpg"></a> <div class="desc"><a href="https://baike.baidu.com/item/%E6%A0%80%E5%AD%90%E8%8A%B1%E5%BC%80/15839944">栀子花开</a> </div> </div> <div class="img"> <a herf="https://baike.baidu.com/item/%E6%9D%8E%E6%98%93%E5%B3%B0/3908480?fr=aladdin"> <img src="https://gss0.bdstatic.com/94o3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike116%2C5%2C5%2C116%2C38/sign=e95988cb652762d09433acedc185639f/bf096b63f6246b604c86859cedf81a4c500fa249.jpg"></a> <div class="desc"><a href="https://baike.baidu.com/item/%E8%80%81%E7%82%AE%E5%84%BF/15891954">老炮儿</a></div> </div> <br> </div> <nav class="navbar navbar-default navbar-fixed-bottom" role="navigation"> <div class="container-fluid"> <div class="navbar-header"> <a class="navbar-brand" href="http://music.baidu.com/artist/1370">音乐作品</a> </div> <div> <ul class="nav navbar-nav"> <li class="active"><a href="http://music.baidu.com/?fr=baikePC">热门歌曲</a></li> <li><a href="https://baike.baidu.com/item/%E6%98%93%E5%B3%B0%E6%83%85%E4%B9%A6/15815910">专辑</a></li> <li class="active"><a href="http://bd.kuwo.cn/mingxing/%E6%9D%8E%E6%98%93%E5%B3%B0.htm?from=baidu">单曲</a></li> </li> </ul> </div> </div> </nav> <footer> <div class="foot"> <div><a href="#"> 点我呀</a> · <a href="#"> 加入我们</a> · <a href="#"> 歌曲或歌名 </a> · <a href="#">帮助中心</a> · <a href="#">合作伙伴</a></div> <div>中国大陆男演员</div> </div> </footer>
结果:
2、在父模板中更新导航,插入登录状态判断代码。、
注意用{% ... %}表示指令。
{{ }}表示变量
3、完成注销功能。
清除session
跳转
父模板:
{% 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\'))
结果:
以上是关于登录之后更新导航(2017.11.24)的主要内容,如果未能解决你的问题,请参考以下文章