• 作业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
  • 相关阅读:
    [转]C# 常用字符串加密解密方法
    ADO.net商机题目
    ADO.net属性拓展
    实体类,数据访问类应用
    实体类,数据访问类.字符串攻击.防攻击
    ADO.net增删改的应用
    ADO.net增删改查
    css——层叠样式表
    标题栏小图标、锚点、滚动效果、视频插入、音乐插入
    HTML——超文本标记语言(表单及12个表单元素)
  • 原文地址:https://www.cnblogs.com/888abc/p/7875528.html
Copyright © 2020-2023  润新知