登录之后更新导航

  1. 用上下文处理器app_context_processor定义函数
    1. 获取session中保存的值
    2. 返回字典
  2. 在父模板中更新导航,插入登录状态判断代码。
    1. 注意用{% ... %}表示指令。
    2. {{ }}表示变量
  3. 完成注销功能。
    1. 清除session
    2. 跳转
      from flask import Flask,render_template,request,redirect,url_for,session
      from flask_sqlalchemy import SQLAlchemy
      import config
      from functools import wraps
      from datetime import datetime
      
      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))
      
      # db.create_all()
      
      # 添加/删除
      # user=User.query.filter(User.username=='mis1',password='mis1')
      # db.session.add/delete(user)
      # db.session.commit()
      # 查询
      # user=User.query.filter(User.username =='mis1').first()
      # print(user.username,user.password)
      # 修改
      # user=User.query.filter(User.username=='mis1').first()
      # user.password='123456'
      # user.username='mis1'
      # print(user.username,user.password)
      # db.session.commit()
      
      @app.route('/')
      def base():
          return render_template('base.html')
      
      @app.route('/regist/',methods=['GET','POST'])
      def regist():
          if request.method=='GET':
              return render_template('regist.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 'username existed'
              else:
                  user=User(username=username,password=password,nickname=nickname)
                  db.seesion.add(user)#数据库操作
                  db.seesion.commit()
                  return redirect(url_for('login'))
      
      
      @app.route('/login/',methods=['GET','POST'])
      def login():
          if request.method=='GET':
              return render_template('login.html')
          else:
              username2=request.form.get('username')
              password2=request.form.get('password')
              user = User.query.filter(User.username == username2,User.password==password2).first()
              if user:
                  session['user'] == username2
                  session.permanent = True
                  return render_template(url_for('base'))
              else:
                      return u'error username or password'
              #     if user.password==password2:
              #         return redirect(url_for('base'))
              #     else:
              #         return u'password error'
              # else:
              #     return u'username is not existed.'
      
      
      @app.context_processor
      def mycontext():
          username2=session.get('user')
          if username2:
              return {'username':username2}
          else:
              return {}
      
      @app.route('/ask/')
      def ask():
          return render_template('ask.html')
      
      @app.route('/logout/')
      def logout():
          session.clear()
          return  redirect(url_for(base))
      
      
      
      if __name__=='__main__':
          app.run()
      <!DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <title>
              {% block title %}{% endblock %}
              首页
          </title>
      
          <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/base.css') }}">
          <script src="{{ url_for('static',filename='js/base.js') }}"></script>
      {% block head %}
      {% endblock %}
      
      </head>
      
      <body id="myBody">
      
          <nav>
          <img src="http://p1.img.cctvpic.com/photoAlbum/page/performance/img/2013/8/27/1377583188891_732.jpg" width="800px"height="150px"><br>
      
              <img id="myOnOff" οnclick="mySwitch()" src="http://www.runoob.com/images/pic_bulbon.gif" width="25px">
       <p>{{ username }}contextx</p>
          <a href="http://www.sesamestreetchina.com.cn/">首页</a>
          <input type="text"name="search">
          <button type="submit">搜索</button>
        <a href="{{ url_for('ask') }}">发布问题</a>
           {% if username %}
              <li><a href="#"{{username}}></a></li>
              <li><a href="{{url_for('logout')}}">注销</a>></li>
           {% else %}
              <li><a href="{{ url_for('login') }}">登录</a></li>
              <li><a href="{{ url_for('regist') }}">注册</a></li>
           {% endif %}
      
              <img src="{{ url_for('static',filename='image/86x97sZ11K4.jpg') }}" alt="" width="50px">
      
      
      </nav>
      
      {% block main %}
      
      {% endblock %}
      
      
      <div class="area">
      </div>
      
      <footer>
          <div class="footer_box">
              Copyright@2017 yangjlee </div>
          </footer>
      
      
      </body>
      </html>

       

转载于:https://www.cnblogs.com/yangjlee/p/7889832.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值