登录之后更新导航

  1. 用上下文处理器app_context_processor定义函数
    1. 获取session中保存的值
    2. 返回字典
  2. 在父模板中更新导航,插入登录状态判断代码。
    1. 注意用{% ... %}表示指令。
    2. {{ }}表示变量
  3. 完成注销功能。
    1. 清除session
    2. 跳转
      <!DOCTYPE html>
      <html>
      <head>
      <meta charset="utf-8">
      <title>{%block title %}{% endblock %}导航</title>
          <link rel="stylesheet" type="text/css" href="{{url_for('static',filename='css/daohang.css')}}">
      </head>
      <body bgcolor="#00ffff">
      <ul>
          <li><a class="active" >首页</a></li>
          <li><a >新闻</a></li>
          <li><a >图库</a></li>
          <li><a >关于</a></li>
          <input class="eat" type="text" name="sousuo" value="请输入内容">
          <input class="eat"type="button" name="submit" value="搜索">
          
                  {% if username %}
                          <li><a href="#">{{ username }}</a></li>
                          <li><a href="{{ url_for('base')}}">注销</a></li>
                  {% else %}
                          <li><a href="{{ url_for('login')}}">登录</a></li>
                          <li><a href="{{ url_for('zhuce')}}">注册</a></li>
                  {% endif %}
      </ul>
      {% block main %}{% endblock %}
      {% block ma %}{% endblock %}
      </body>
      </html>
      {% extends'daohang.html' %}
      {% block title %}
      <h1>登录</h1>
      {% endblock %}
      {% block main %}
          <head>
       <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/denglu.css') }}">
          <script src="../static/js/denglu.js"></script>
      </head>
      <body>
      <div class="one"> <h2>登录</h2>
              <p>{{ username }}contextx</p>
              <form action="{{ url_for('deng') }}"method="post">
             <div class="input_box"  >
                 <input id="uname" type="text" placeholder="请输入用户名">
             </div>
             <div class="input_box">
                 <input id="upass" type="password" placeholder="请输入密码">
             </div>
             <div id="error_box"><br></div>
         <div class="input_box">
             <button onclick="myLogin()">登录</button>
         </div>
      </form>
        </div>
      </div>
      </body>
      {% endblock %}
      from flask import Flask,render_template,request,redirect,url_for,session
      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(20))
      
      db.create_all()
      # user=User(username='mis1234',password='111111')
      # db.session.add(user)
      # db.session.commit()
      
      
      @app.route('/')
      def base():
          return render_template("base.html")
      
      
      @app.route('/login/', methods=['GET', 'POST'])
      def login():
          if request.method == 'GET':
              return render_template("denglu.html")
          else:
              username = request.form.get('username')
              password = request.form.get('password')
              user = User.query.filter(User.username == username).first()
              if user:
                  if user.password == password:
                      return redirect(url_for('base'))
                  else:
                      return '密码错误'
              else:
                  return '用户名不存在'
      
      @app.route('/regist/', methods=['GET', 'POST'])
      def zhuce():
          if request.method == 'GET':
              return render_template("zhuce.html")
          else:
              username = request.form.get('username')
              password = request.form.get('password')
              nickname = request.form.get('nickname')
              user = User.query.filter(User.username == username).first()
              if user:
                  return '用户名已存在'
              else:
                  user = User(username=username, password=password, nickname=nickname)
                  db.session.add(user)  # 数据库,添加操作
                  db.session.commit()
                  return redirect(url_for('login'))
      
      @app.context_processor
      def mycontext():
          usern=session.get('user')
          if usern:
              return{'username':usern}
          else:
              return {}
      
      @app.route('/logout/')
      def logout():
          session.clear()
          return redirect(url_for('base'))
      
      
      @app.route('/fabu/')
      def fabu():
          return  render_template('question.html')
      
      if __name__=='__main__':
          app.run(debug=True)
      SQLALCHEMY_DATABASE_URI='mysql+pymysql://root:@127.0.0.1:3306/test?charset=utf8'
      SQLALCHEMY_TRACK_MODIFICATIONS = False

       

posted on 2017-11-24 16:07  083李笑晴  阅读(132)  评论(0)    收藏  举报

导航