码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-11-22 17:40:08      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:错误   用户名   mys   hold   ext   word   输入密码   表单   end   

登录功能完成:

  1. js:设置return
  2. html:设置
    1. form
    2. input
    3. onclick="return fnLogin()"
  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
    import os
    DEBUG = True
    
    SECRET_KEY = os.urandom(24)
    
    DIALECT = ‘mysql‘
    DRIVER = ‘mysqldb‘
    USERNAME = ‘root‘
    PASSWORD = ‘ROOT‘
    HOST = ‘127.0.0.1‘
    DATABASE = ‘mytest‘
    
    
    SQLALCHEMY_DATABASE_URI = ‘mysql+pymysql://root:123456@127.0.0.1:3306/mytest?charset=utf8‘
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    <!DOCTYPE html>
    <html lang="en">
    <head>
        {% extends ‘test1.html‘ %}
        <meta charset="UTF-8">
        <title>{% block title %}
        登录界面
        {% endblock %}
        </title>
    {% block head %}
    <link rel="stylesheet" type="text/css" href="../static/css/denlu.css">
        <script src="../static/js/return.js"></script>
    {% endblock %}
    
    
    </head>
    <body>
    {% block body %}
        <div class="box">
            <h1>登录</h1>
            <form action="{{ url_for(‘deng‘) }}" method="post">
                  <div class="input_box">
                <input id="name" type="text" placeholder="请输入用户名" name="username">
            </div>
            <div class="input_box">
                <input id="pass" type="password" placeholder="请输入密码" name="password">
            </div>
            <div id="error_box"><br>
            </div>
            <div class="input_box">
                <button onclick="fnLogin()">登录</button>
            </div>
            </form>
    
        </div>
    {% endblock %}
    
    </body>
    </html>
    function fnLogin() {
        var oUname = document.getElementById("name");
        var oUpass = document.getElementById("pass");
        var oError = document.getElementById("error_box");
        var isError = true;
        oError.innerHTML = ‘<br>‘
        //
        if (oUname.value.length > 20 || oUname.value.length < 6) {
            oError.innerHTML = "name:6-20位";
            isError = false;
            return isError;
        } else if ((oUname.value.charCodeAt(0) >= 48) && (oUname.value.charCodeAt(0) <= 57)) {
            oError.innerHTML = "first letter.";
            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(0) < 97) || (oUname.value.charCodeAt(0) > 122)) {
                oError.innerHTML = "only letter or number";
                isError = false;
                return isError;
            }
    
        }
    
        if (oUpass.value.length > 20 || oUpass.value.length < 6) {
            oError.innerHTML = "password:6-20";
            isError = false;
            return isError;
    
        }
        return isError;
        window.alert("登录成功!")
    }

     

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

标签:错误   用户名   mys   hold   ext   word   输入密码   表单   end   

原文地址:http://www.cnblogs.com/caoxinwen092/p/7879715.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!