登陸功能完成:css
onclick="return fnLogin()"
session:html
from flask import Flask,render_template,request,redirect,url_for,session from flask_sqlalchemy import SQLAlchemy import config app = Flask(__name__) app.config.from_object(config) db = SQLAlchemy(app) class User(db.Model): __tablename__ = 'user' 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) #db.create_all() #user=User(username='yyyy1',password='111111') #db.session.add(user) #db.session.commit() #user = User.query.filter(User.username == 'yyyy1').first() #user=User.query.filter(User.username=='yyyy1').first() #user.password='234567' #print(user.username,user.password) #db.session.commit() #user=User.query.filter(User.username == 'yyyy1').first() #db.session.delete(user) #db.session.commit() # @app.route('/') def base2(): return render_template('base2.html') @app.route('/login2/', methods=['GET','POST']) def login2(): if request.method=='GET': return render_template('login2.html') else: usern=request.form.get('username') passw= request.form.get('password') user=User.query.filter(User.username==usern).first() if user: if user.password == passw: session['user'] = usern return redirect(url_for('base2')) else: return "密碼錯誤,請從新輸入。" else: return "用戶名不存在!" @app.route('/register2/', methods=['GET','POST']) def register2(): if request.method=='GET': return render_template('register2.html') else: usern=request.form.get('username') passw= request.form.get('password') user=User.query.filter(User.username==usern).first() if user: return 'username existed' else: user=User(username=usern,password=passw) db.session.add(user) db.session.commit() return redirect(url_for('login2')) if __name__ == '__main__': app.run(debug=True)
import os SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:@127.0.0.1:3306/mis_db?charset=utf8' SQLALCHEMY_TRACK_MODIFICATIONS = False SECRET_KEY = os.urandom(24)
{% extends'base2.html' %} {% block title %} Login {% endblock %} {% block head %} <link href="{{ url_for("static",filename="css/1.css") }}" rel="stylesheet" type="text/css"> <script src="{{ url_for("static",filename="js/denglu.js") }}"></script> {% endblock %} {% block main %} <body> <div class="box"> <form action="{{ url_for('login2') }}" method="post"> <div id="container" style="width: 400px"align="center"> <div id="header" style="background-color:lightskyblue"><h2 align="center" style="margin-bottom: 0;">會員登陸</h2></div> <div id="content" style="background-color:lightpink;height:205px;width:400px;float:left;"> <p></p> <div class="input_box"> Username:<input id="username"type="text" name="username"placeholder="請輸入用戶名" name="username"><br> </div> <br> <div class="input_box"> Password:<input id="password"type="password" name="password"placeholder="請輸入登陸密碼" name="password"><br> </div> <div id="error_box"><br></div> <div class="input_box"> <input type="radio">普通會員 <input type="radio">金牌會員 <input type="radio">鑽石會員 </div><br> <div class="input_box"> <button onclick="return fnLogin()">登陸</button> <button type="button" onclick=window.alert("是否取消登陸!")>取消</button> </div> </div> </div> </form> </div> </div> </body> {% endblock %} </html>
function fnLogin() { var oUname = document.getElementById("username") var oError = document.getElementById("error_box") var opassward = document.getElementById("password") var isError=true; oError.innerHTML="<br>" //uname 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="用戶名只能是字母或數字"; isError = false; return isError; } } //upass if(oUpass.value.length<6 || oUpass.value.length>12){ oError.innerHTML="密碼6-12位"; isError = false; return isError; } window.alert("登陸成功") }