登录之后更新导航

  1. 用上下文处理器app_context_processor定义函数
    1. 获取session中保存的值
    2. 返回字典
    3. 处理器
      @app.context_processor

      def mycontext(): username
      = session.get('user') if username: return {'username': username} else: return {}

       

  2. 在父模板中更新导航,插入登录状态判断代码。
    1. 注意用{% ... %}表示指令。
    2. {{ }}表示变量
      from flask import Flask, render_template,request,redirect,url_for
      from flask_sqlalchemy import SQLAlchemy
      import config
      
      app = Flask(__name__)
      app.config.from_object(config)
      db = SQLAlchemy(app)
      
      
      class User(db.Model):
          __tablename__ = 'user'
          id = db.Column(db.Integer, primary_key=True, autoincrement=True)
          username = db.Column(db.String(20), nullable=False)
          password = db.Column(db.String(20), nullable=False)
          nickname = db.Column(db.String(50))
      
      #增
      #user = User(username ='ccccls',password='111111' )
      #db.session.add(user)
      #db.session.commit()
      #查
      #user=User.query.filter(User.username =='ccccls').first()
      #print(user.username,user.password)
      #删
      #user=User.query.filter(User.username =='ccccls').first()
      #db.session.delete(user)
      3db.session.commit()
      #改
      #user=User.query.filter(User.username =='ccccls').first()
      #user.password='ccccls'
      #db.session.commit()
      #db.create_all()
      
      @app.route('/base/')
      def base():
          return render_template('base.html')
      
      
      @app.route('/login/')
      def login():
          return render_template('login.html')
      
      
      @app.route('/register/', methods=['GET','POST'])
      def register():
          if request.method=='GET':
              return render_template('register.html')
          else:
              usern=request.form.get('username')
              nickn = request.form.get('nickname')
              passw= request.form.get('password')
              user=User.query.filter(User.username==username).first()
              if user:
                  return u'username existed'
              else:
                  user=User(username=username,password=password)
                  db.session.add(user)
                  db.session.commit()
                  return redirect(url_for('login'))
      
      
      @app.route('/question/')
      def question():
          return render_template('question.html')
      
      
      @app.route('/index/')
      def index():
          return render_template('index.html')
      
      
      if __name__ == '__main__':
          app.run(debug=True)

       


      </nav> {% block main %} <p>{{ username }}xyjie</p> {% endblock %}

      <link rel="stylesheet" type="text/css" href="xyjie.css">

      <link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css">
      </head>
      <body id="mybody">
      
      <nav class="navbar navbar-inverse" role="navigation">
          <div class="container-fluid">
              <div class="navbar-header">
                  <a class="navbar-brand" href="#">首页</a>
              </div>
              <div>
                  <ul class="nav navbar-nav">
                      <li><a href="#" onclick="">登陆</a></li>
                      <li><a href="#" onclick="">注册</a></li>
                      <li class="dropdown">
                          <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                              设置 <b class="caret"></b>
                          </a>
                          <ul class="dropdown-menu">
                              <li><a href="#">收藏</a></li>
                              <li><a href="#">分享</a></li>
                              <li><a href="#">搜索</a></li>
                              <li class="divider"></li>
                              <li><a href="#">点赞</a></li>
                          </ul>
                      </li>
                      <li><a href="#">||</a></li>
                  </ul>
              </div>
              <div style="float: left">
                  <img id="myonoff" onclick="mySwitch()" src="http://p0.so.qhimgs1.com/bdr/200_200_/t01d734a105379159dc.gif" style width="40px"  >
              </div>
              <div>
                  <input type="text" name="user" id="user" placeholder="请输入内容">
                  <input type="button" value="搜索" class="btn btn-success" onclick="">
              </div>
          </div>
      </nav>
      
      <nav class="navbar navbar-inverse navbar-fixed-bottom" role="navigation">
          <ul class="nav navbar-nav">
              <li><img src="http://www.gzcc.cn/2016/images/footer1-logo.png"></li>
              <li style="color: bisque;">版权所有@xyjie</li>
          </ul>
      </nav>
      
      <div>
          <div class="sa">
              <a href="http://desk.zol.com.cn/"><img
                      src="https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2330928080,25031233&amp;fm=27&amp;gp=0.jpg"></a>
              <div class="st"><a href="http://desk.zol.com.cn/">花花华晨宇1</a></div>
          </div>
          <div class="sa">
              <a href="http://desk.zol.com.cn/"><img
                      src="https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3605928156,2200570185&amp;fm=27&amp;gp=0.jpg"></a>
              <div class="st"><a href="http://desk.zol.com.cn/">花花华晨宇2</a></div>
          </div>
          <div class="sa">
              <a href="http://desk.zol.com.cn/"><img
                      src="https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2787456122,2827006597&amp;fm=27&amp;gp=0.jpg"></a>
              <div class="st"><a href="http://desk.zol.com.cn/">花花华晨宇3</a></div>
          </div>
          <div class="sa">
              <a href="http://desk.zol.com.cn/"><img
                      src="https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2478028302,36965079&amp;fm=27&amp;gp=0.jpg"></a>
              <div class="st"><a href="http://desk.zol.com.cn/">花花华晨宇4</a></div>
          </div>
      </div>
      </footer>
       

       

  3. 完成注销功能。
    1. 清除session
    2. 跳转     
      @app.route('/logout/')
      def logout():
          session.clear()
          return redirect(url_for('base'))
       

       

    3.  

    4.  

posted @ 2017-11-24 21:15  073徐英杰  阅读(247)  评论(0编辑  收藏  举报