码迷,mamicode.com
首页 > 其他好文 > 详细

登录之后更新导航

时间:2017-11-24 22:50:42      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:app   处理器   清除   text   get   处理   获取   ssi   redirect   

  1. 用上下文处理器app_context_processor定义函数
    1. 获取session中保存的值
    2. 返回字典
      @app.context_processor
      def mycontext():
          username = session.get(user)
          if username:
              return {username:username}
          else:
              return {}

       

  2. 在父模板中更新导航,插入登录状态判断代码。
    1. 注意用{% ... %}表示指令。
    2. {{ }}表示变量
      {% if username %}
          <li><a href="#"style="color: red" >{{ username }}</a></li>
          <li><a href="{{url_for(‘logout‘)}}"style="color: red" >注销</a></li>
          {% else %}
          <a href="http://127.0.0.1:5000/login" style="color: red">登录</a>
          <a href="{{ url_for(‘regist‘)}}" style="color: red">注册</a>
          {% endif %}

       

  3. 完成注销功能。
    1. 清除session
    2. 跳转
      @app.route(/logout/)
      def logout():
          session.clear()
          return redirect(url_for(hh))
      {% if username %}
          <li><a href="#"style="color: red" >{{ username }}</a></li>
          <li><a href="{{url_for(‘logout‘)}}"style="color: red" >注销</a></li>
          {% else %}
          <a href="http://127.0.0.1:5000/login" style="color: red">登录</a>
          <a href="{{ url_for(‘regist‘)}}" style="color: red">注册</a>
          {% endif %}

       

登录之后更新导航

标签:app   处理器   清除   text   get   处理   获取   ssi   redirect   

原文地址:http://www.cnblogs.com/00js/p/7892472.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!