• 完成登录功能,用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
    function fndenglu() {
        var oUname = document.getElementById("uname");
        var oUpass = document.getElementById("upass");
        var oError = document.getElementById("error_box");
        var isError = true;
        oError.innerHTML = "<br>";
        // oUname
        if (oUname.value.length < 6 || oUname.value.length > 20) {
            oError.innerHTML = "用户名要6-20之位";
            isError = false;
            return isError;
        } else if ((oUname.value.charCodeAt(0) >= 48) && (oUname.value.charCodeAt(0) <= 57)) {
            oError.innerHTML = "首字母不能为数字";
            isError = false;
            return isError;
        } 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 = "只能填写数字或字母";
                isNotError = false;
                return isError;
            }
        }
    // oUpass
        if (oUpass.value.length > 20 || oUpass.value.length < 6) {
            oError.innerHTML = "密码要6-20位";
            isError = false;
            return isError;
        }
        return isError;
        window.alert("登录成功!")
    }
    {% extends 'daohang.html' %}
    {% block title %}登录{% endblock %}
    {% block head %}
        <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/denglu.css') }}">
        <script src="{{ url_for('static',filename='js/denglu.js') }}"></script>
    {% endblock %}
    
    {% block main %}
        <div class="box">
    
            <h2>登录</h2>
            <form action="{{ url_for('denglu') }}" method="post">
                <div class="input_box">
                    <input id="uname" type="text" placeholder="请输入用户名" name="username">
                </div>
                <div class="input_box">
                    <input id="upass" type="password" placeholder="请输入密码" name="password">
                </div>
                <div id="error_box"><br></div>
                <div class="input_box">
                    <button onclick="fndenglu()">登录</button>
                </div>
            </form>
        </div>
    {% endblock %}
    @app.route('/denglu/', methods=['GET', 'POST'])
    def denglu():
        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,User.password==password).first()
            if user:
                session['user']=username
                session.permanent=True
                return redirect(url_for('daohang'))
            else:
                return 'error username or password'
    import os
    
    DEBUG=True
    
    SECRET_KEY=os.urandom(24)
    
    DIALECT='mysql'
    DRIVER='mysqldb'
    USERNAME='root'
    PASSWORD=''
    HOST='127.0.0.1'
    PORT='3306'
    DATABASE='test'
    
    
    SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:@127.0.0.1:3306/test?charset=utf8'
    SQLALCHEMY_TRACK_MODIFICATIONS = False
  • 相关阅读:
    设计模式之三:Abstract Factory(转)
    设计模式之二:adapter模式(转)
    设计模式之一:设计原则(转)
    双链表操作
    单链表操作
    C#-Activex插件操作指南
    积分源码上线
    換友情鏈接
    企业短信群发
    掉了,全掉了。
  • 原文地址:https://www.cnblogs.com/xiaojiaqi/p/7871979.html
Copyright © 2020-2023  润新知