1. 程式人生 > >作業27——登錄之後更新導航

作業27——登錄之後更新導航

技術分享 html content 分享圖片 分享 color logs red 模板

  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——登錄之後更新導航