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

Posted Leheartset

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了完成登录功能,用session记住用户名相关的知识,希望对你有一定的参考价值。

1.登录功能完成:

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

session:

    1. 从`flask`中导入`session`
    2. 设置`SECRET_KEY`
    3. 操作字典一样操作`session`:增加用户名`session[‘username‘]=`username

 

login.html

<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="../static/js/login.js">

</script>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" type="text/css" href="../static/css/style.css">
    <link href=‘http://fonts.googleapis.com/css?family=Oleo+Script‘ rel=‘stylesheet‘ type=‘text/css‘>

</head>
<body>
<div class="lg-container" >
    <h1>登录</h1>
    <form action="{{ url_for(‘login‘) }}" id="lg-form" name="lg-form" method="post">

        <div>
                <label for="username">Username:</label>
                <input id="uname" type="text" name="用户名" placeholder="请输入用户名"><br>
        </div>

        <div>
        <label for="password">Password:</label>
              <input class="password" id="upass" type="password" name="密码" placeholder="请输入密码" >
        </div>
        <input  type="checkbox" name="vehicle" value="true"><span>记住密码</span>
        <a class="right" href="">登录遇到问题?</a>

        <div id="error_box"></div>
        <button type="submit" value="登录" id="login" onclick="return fnlogin()">登录</button>

    </form>
    <div id="message"></div>
</div>

</body>
</html>

login.js

ar oUname = document.getElementById("uname");
    var oUpass = document.getElementById("upass");
    var oUpass2 = document.getElementById("upass2");
    var oError = document.getElementById("error_box");
    var isError = true;
    oError.innerHTML = "<br>";
    if (oUname.value.length < 6 || oUname.value.length > 12) {
        oError.innerHTML = "用户名6-12位";
        isError = false;
        // noinspection JSAnnotator
        return isError;
    } else if ((oUname.value.charCodeAt(0) >= 48) && (oUname.value.charCodeAt(0) <= 57)) {
        oError.innerHTML = "first letter";
        isError = false;
        // noinspection JSAnnotator
        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 = "only letter or number";
                isError = false;
                // noinspection JSAnnotator
                return isError;
            }

        }

myqa.py

from flask import Flask, request, render_template,redirect,url_for,session
from flask_sqlalchemy import SQLAlchemy
import config
# from functools import wraps
# from datetime import  datetime

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(username =‘vixx‘,password=‘20120524‘ )
# db.session.add(user)
# db.session.commit()
# db.create_all()

@app.route(/)
def index():
    return render_template(homepage.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==usern).first()
        if user:
            session[user]=usern
            session.permanent=True
            if user.password==passw:
                return redirect(url_for(homepage))
            else:
                return upassword error
        else:
            return uusername is not existed


@app.route(/regist/,methods=[GET,POST])
def regist():
    if request.method==GET:
        return render_template(regist.html)
    else:
        username = request.form.get(username)
        password = request.form.get(password)
        nickname = request.form.get(nickname)
        user =User.query.filter(User.username==username).first()
        if user:
            return  username existed
        else:
            user=User(username=username,password=password,nickname=nickname)
            db.session.add(user)   #数据库操作
            db.session.commit()
            return redirect(url_for(login))


if __name__ == __main__:
    app.run()

 

以上是关于完成登录功能,用session记住用户名的主要内容,如果未能解决你的问题,请参考以下文章

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

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

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

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

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

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