• 作业26——完成登录功能,用session记住用户名


    登录功能完成:

    1. js:设置return

    regist.js

    function fnRegist() {
        var zcoUname = document.getElementById("zcuname")
        var zcoError = document.getElementById("zcerror_box")
        var zcoUword1 = document.getElementById("zcuword1")
        var zcoUword2 = document.getElementById("zcuword2")
    
        zcoError.innerHTML = "<br>"
        if (zcoUname.value.length < 6 || zcoUname.value.length > 12) {
            zcoError.innerHTML = "用户名为6到12位";
            return false;
        } else if ((zcoUname.value.charCodeAt(0) >= 48) && (zcoUname.value.charCodeAt(0) <= 57)) {
            zcoError.innerHTML = "用户名首位不能是数字";
            return false;
        } else for (var i = 0; i < zcoUname.value.length; i++) {
            if ((zcoUname.value.charCodeAt(i) < 48) || (zcoUname.value.charCodeAt(i) > 57) && (zcoUname.value.charCodeAt(i) < 97) || (zcoUname.value.charCodeAt(i) > 122)) {
                zcoError.innerHTML = "用户名只能是字母与数字";
                return false;
            }
        }
        if ((zcoUword1.value.length < 6) || (zcoUword1.value.length > 20)) {
            zcoError.innerHTML = "密码为6到20位";
            return false;
        }
        else if (zcoUword2.value.length == 0) {
            zcoError.innerHTML = "请再次输入密码";
            return false;
        }
        else if (zcoUword1.value != zcoUword2.value) {
            zcoError.innerHTML = "两次密码不一致"
            return false;
        }
        return true;
    }

    login.js

    function fnLogin() {
        var oUname = document.getElementById("uname")
        var oError = document.getElementById("error_box")
        var oUword = document.getElementById("uword")
    
        oError.innerHTML = "<br>"
        if (oUname.value.length < 6 || oUname.value.length > 12) {
            oError.innerHTML = "用户名为6到12位";
            return false;
        } else if ((oUname.value.charCodeAt(0) >= 48) && (oUname.value.charCodeAt(0) <= 57)) {
            oError.innerHTML = "用户名首位不能是数字";
            return false;
        } else for (var i = 0; i < oUname.value.length; i++) {
            if ((oUname.value.charCodeAt(i) < 48) || (oUname.value.charCodeAt(i) > 57) && (oUname.value.charCodeAt(i) < 97) || (oUname.value.charCodeAt(i) > 122)) {
                oError.innerHTML = "用户名只能是字母与数字";
                return false;
            }
        }
    
        if ((oUword.value.length < 6) || (oUword.value.length > 20)) {
            oError.innerHTML = "密码为6到20位";
            return false;
        }
       return true;
    }
    1. html:设置
      1. form
      2. input
    <form action="{{ url_for('regist') }}" method="post" onsubmit="return fnRegist()"> 
    ......
    <div class="zcinput_box">
    <input type="submit" value="立即注册"></input>
    </div>
    </form>
    1. py:
      1. @app.route设置methods
      2. GET
      3. POST
        1. 读取表单数据
        2. 查询数据库
          1. 用户名密码对:
            1. 记住用户名
            2. 跳转到首页
          2. 用户名密码不对:
            1. 提示相应错误。
    @app.route('/login/', methods=['GET', 'POST'])
    def login():
        if request.method=='GET':
            return render_template('login.html')
        else:
            usern = request.form.get('username')
            passw = request.form.get('password')
            user = User.query.filter(User.username == usern,User.password==passw).first()
        if user:
            session['user']=usern
            session.permanent=True
            return redirect(url_for('index'))
    
        else:
            return u'用户名不存在/密码错误'
    
    
    
    @app.route('/regist/', methods=['GET', 'POST'])
    def regist():
        if request.method == 'GET':
            return render_template('regist.html')
        else:
            # 获取form中的数据,判断用户名是否存在,存到数据库中
            username = request.form.get('username')
            nickname = request.form.get('nickname')
            password = request.form.get('password')
            user = User.query.filter(User.username == username).first()
            # 重定向到登录页
            if user:
                return u'用户名已存在'
            else:
                user = User(username=username, nickname=nickname, password=password)
                db.session.add(user)
                db.session.commit()
                return redirect(url_for('login'))

    session:

    1. 从`flask`中导入`session`
    rom flask import Flask, render_template, request, redirect, url_for,session
    1. 设置`SECRET_KEY`
    import os
    
    DEBUG=True
    SECRET_KEY=os.urandom(24)
    
    DIALEXT='mysql'
    DRIVER='mysqldb'
    USERNAME='root'
    PASSWORD='ROOT'
    HOST='@localhost'
    PORT='3306'
    DATABASE='meow'
    1. 操作字典一样操作`session`:增加用户名`session['username']=`username
  • 相关阅读:
    Java Sound : generate play sine wave
    Java Sound : audio inputstream from pcm amplitude array
    Silence Removal and End Point Detection MATLAB Code
    Java extract amplitude array from recorded wave
    sqlserver中sp_executesql使用实例(获取动态sql输出结果)
    js字母大小写字母转换
    String.Format数字格式化输出 {0:N2} {0:D2} {0:C2}
    System.Data.SqlClient.SqlError:无法打开备份设备'D:..abc.bak'
    Loadrunner监控windows系统“找不到网络路径”问题解决
    未能加载文件或程序集“System.WEB.DataVisualization, Version=3.5.0.0, Culture=neutral......
  • 原文地址:https://www.cnblogs.com/888abc/p/7875528.html
Copyright © 2020-2023  润新知