完成登陸功能,用session記住用戶名

登陸功能完成:html

  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:mysql

  1. 從`flask`中導入`session`
  2. 設置`SECRET_KEY`
  3. 操做字典同樣操做`session`:增長用戶名`session['username']=`username

鏈接頁面git

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


app = Flask(__name__)
app.config.from_object(sqlconnect)#app鏈接數據庫
db = SQLAlchemy(app)

class User(db.Model):
    __tablename__ ='user1'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    username = db.Column(db.String(20),nullable=False)
    password = db.Column(db.String(20), nullable=False)
    #againpass = db.Column(db.String(20), nullable=False)


#db.create_all()

#增長
# user = User(username ='xxxxx1',password ='123456')
# db.session.add(user)
# db.session.commit()
#查找
# user = User.query.filter(User.username =='xxxxx1').first()
# user.password ='123456'
# user.againpass='123456'
# print(user.id,user.username,user.password)
#刪除
# user = User.query.filter(User.username =='xxxxx1').first()
# db.session.delete(user)
# db.session.commit()

@app.route('/')
def base():
    return render_template("base.html")

@app.route("/login",methods=['GET','POST'])
def sign_in():
    if request.method =='GET':
        return render_template("sign_in.html")
    else:
        name = request.form.get('name')
        passw = request.form.get('password')
        user = User.query.filter(User.username == name).first()
        #判斷用戶名是否存在
        if user:
            #判斷密碼是否正確
            if user.password == passw:
                session['user']=name#記住用戶名
                return redirect(url_for('base'))
            else:
                return u'密碼錯誤!'
        else:
            return u'不存在該用戶名!'


@app.route("/regiter",methods=['GET','POST'])
def sign_up():
    if request.method == 'GET':
        return render_template("sign_up.html")
    else:
        name = request.form.get('newname')
        passw = request.form.get('newpass')
        samename = User.query.filter(User.username == name).first()
        if samename:
            return u'username existed!'

        else:
            # 將用戶名和密碼存入數據庫
            users =User(username = name ,password = passw)
            db.session.add(users)
            db.session.commit()
            return render_template("sign_in.html")#註冊成功後跳轉到登陸界面


@app.route("/question")
def question():
    return render_template("wenda.html")


if __name__ == '__main__':
    app.run(debug=True)

鏈接數據庫sql

import os

SQLALCHEMY_DATABASE_URI='mysql+pymysql://root:@127.0.0.1:3306/data?charset=utf8'
SQLALCHEMY_TRACK_MODIFICATIONS =False

SECRET_KEY=os.urandom(24)

登陸.js頁面 增長return false數據庫

function myLogin(){
            var uName = document.getElementById("name");
            var uPass = document.getElementById("pass");
            var uError = document.getElementById("error_box");


            uError.innerHTML="<br>"
            //用戶名
            if(uName.value.length<6 && uName.value.length>12) {
                uError.innerHTML = "User name must be between 6 and 12 digits!";
                return false;
            }
           else if ((uName.value.charCodeAt(0)>=48)&&(uName.value.charCodeAt(0)<=57)){
                uError.innerHTML = "The first letter can not be a number!";
                return false;
            }
            else for(var i=0;i<uName.value.length;i++){
                if((uName.value.charCodeAt(i)<48 || uName.value.charCodeAt(i)>57)&&(uName.value.charCodeAt(i)<97 ||uName.value.charCodeAt(i)>122)){
                    uError.innerHTML = "Only number and letter! ";
                    return false;
                }
                }
            //密碼
            if(uPass.value.length>12||uPass.value.length<6){
                uError.innerHTML="The password must be between 6 and 12 digits!";
                return false;
            }
             // window.alert("Successful login!")
            return true;
        }
相關文章
相關標籤/搜索