作業27——登錄之後更新導航
阿新 • • 發佈:2017-11-24
技術分享 html content 分享圖片 分享 color logs red 模板
- 用上下文處理器app_context_processor定義函數
- 獲取session中保存的值
- 返回字典
@app.context_processor def mycontext(): usern=session.get(‘user‘) if usern: return {‘username‘:usern} else: return {}
- 在父模板中更新導航,插入登錄狀態判斷代碼。、
- 註意用{% ... %}表示指令。
- {{ }}表示變量
{#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>
- 完成註銷功能。
- 清除session
- 跳轉
@app.route(‘/logout/‘) def logout(): session.clear() return redirect(url_for(‘index‘))
作業27——登錄之後更新導航