作业27——登录之后更新导航

Posted monmonmonmon

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了作业27——登录之后更新导航相关的知识,希望对你有一定的参考价值。

  1. 用上下文处理器app_context_processor定义函数
    1. 获取session中保存的值
    2. 返回字典
@app.context_processor
def mycontext():
    usern=session.get(\'user\')
    if usern:
        return {\'username\':usern}
    else:
        return {}
  1. 在父模板中更新导航,插入登录状态判断代码。、
    1. 注意用{% ... %}表示指令。
    2. {{ }}表示变量
{#base.html#}
<nav>
    <img id="myOnOff" onclick="mySwitch()"
         src="http://img3.redocn.com/tupian/20150409/shouhuihuangsetaiyangshiliangsucai_4034066.jpg" width="30px">
    <a href="{{ url_for(\'index\') }}">首页</a>
    {% if username %}
        <li><a href="#">{{ username }}</a></li>
        <li><a href="#">注销</a></li>
    {% else %}
        <li><a href="{{ url_for(\'login\') }}">登录</a></li>
        <li><a href="{{ url_for(\'regist\') }}">立即注册</a></li>
    {% endif %}
    <a href="{{ url_for(\'question\') }}">发布问答</a>
    <img src="{{ url_for( \'static\',filename=\'image/meow.jpg\') }}" width="30px">
</nav>
  1. 完成注销功能。
    1. 清除session
    2. 跳转
@app.route(\'/logout/\')
def logout():
    session.clear()
    return redirect(url_for(\'index\'))

 

以上是关于作业27——登录之后更新导航的主要内容,如果未能解决你的问题,请参考以下文章

登录之后更新导航

登录之后更新导航

登录之后更新导航

登录之后更新导航

登录之后更新导航

登录之后更新导航