• 完成登录功能,用session记住用户名


    登录功能完成:

    1. js:设置return
    2. html:设置
      1. form
      2. input
    3. py:
      1. @app.route设置methods
      2. GET
      3. POST
        1. 读取表单数据
        2. 查询数据库
          1. 用户名密码对:
            1. 记住用户名
            2. 跳转到首页
          2. 用户名密码不对:
            1. 提示相应错误。

    session:

    1. 从`flask`中导入`session`
    2. 设置`SECRET_KEY`
    3. 操作字典一样操作`session`:增加用户名`session['username']=`username
    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=TabError, autoincrement=True)
        username = db.Column(db.String(20), nullable=False)
        password = db.Column(db.String(20), nullable=False)
        # nickname = db.Column(db.String(20))
    
    
    # 查询删除
    # user = User.query.filter(User.username == 'lin').first()
    # db.session.delete(user)
    # db.session.commit()
    
    # 修改
    # user = User.query.filter(User.username == 'lin').first()
    # user.password = '1234'
    # print(user.username,user.password)
    # db.session.commit()
    
    # 增加
    # user = User(username = 'lin',password = 'lin')
    # db.session.add(user)
    # db.session.commit()
    
    db.create_all()
    
    
    @app.route('/')
    def jianshu():
        return render_template('jianshu.html')
    
    
    @app.route("/login/", methods=['GET', 'POST'])
    def login():
        if request.method == 'GET':
            return render_template('login.html')
        else:
            username = request.form.get('username')
            password = request.form.get('password')
            user = User.query.filter(User.username == username).first()
            if user:
                session['user'] = username
                session.permanent = True
                return redirect(url_for('jianshu'))
            else:
                return 'error username or password'
    
    
    @app.route("/enroll/", methods=['GET', 'POST'])
    def enroll():
        if request.method == 'GET':
            return render_template('enroll.html')
        else:
            username = request.form.get('username')
            password = request.form.get('password')
            user = User.query.filter(User.username == username).first()
            if user:
                return 'username exited'
            else:
                user = User(username=username, password=password)
                db.session.add(user)
                db.session.commit()
                return redirect(url_for('login'))
    
    
    @app.route("/post/")
    def post():
        return render_template('post.html')
    
    
    if __name__ == '__main__':
        app.run(debug=True)

  • 相关阅读:
    常用的CSS命名规则 (web标准化设计)
    有哪些概率论和数理统计的深入教材可以推荐?
    CV2X国内现状分析
    隐私计算,新能源汽车“安全上路”的“救命稻草”?
    2022年中国车联网行业全景图谱
    2022年十大AI预测:气候独角兽涌现、中美竞争加剧
    OSEK/VDX介绍
    Adaptive Autosar
    基于我国商密算法的车联网5GV2X通信安全可信体系
    行研篇 | 汽车域控制器研究
  • 原文地址:https://www.cnblogs.com/lintingting/p/7872570.html
Copyright © 2020-2023  润新知