登录功能完成:
- js:设置return
function fnLogin() { var oUname = document.getElementById("uname"); var oUpass = document.getElementById("upass"); var oError = document.getElementById("error_box"); oError.innerHTML = "<br>" // uname if (oUname.value.length > 20 || oUname.value.length < 6) { oError.innerHTML = "亲,用户名至少6-20位哦"; return false; } else if ((oUname.value.charCodeAt(0) >= 48) && (oUname.value.charCodeAt(0) <= 57)) { oError.innerHTML = "用户名首字母不能是数字"; return false; } 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 = "用户名只能为字母或数字"; return false; } } // upass if (oUpass.value.length > 20 || oUpass.value.length < 6) { oError.innerHTML = "亲,密码至少6-20位哦"; return false; } window.alert("用户登录成功!") }
- html:设置
- form
- input
-
οnclick="return fnLogin()"
{% extends 'index.html' %} {% block title %}登录{% endblock %} {% block head %} <link href="../static/css/login.css" rel="stylesheet" type="text/css"> <script src="../static/js/login.js"></script> {% endblock %} {% block main %} <body class="bg"> <div id="container"> <div class="box"> <h2 id="denglu">登录</h2> <div class="input_box"> <input type="text" id="uname" placeholder="请输入用户名"> </div> <div class="input_box"> <input type="password" id="upass" placeholder="请输入密码"> </div> <div class="error" id="error_box"><br></div> <div class="input_box"> <button id="login" type="submit" οnclick="fnLogin()">登录</button> <button id="cancel" type="cancel" οnclick="fnLogin()">取消</button> </div> </div> {% endblock %} </body> </html>
- py:
- @app.route设置methods
- GET
- POST
- 读取表单数据
- 查询数据库
- 用户名密码对:
- 记住用户名
- 跳转到首页
- 用户名密码不对:
- 提示相应错误。
- 用户名密码对:
session:
- 从`flask`中导入`session`
- 设置`SECRET_KEY`
- 操作字典一样操作`session`:增加用户名`session['username']=`username
from flask import Flask, render_template,redirect,url_for,request,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) nickname = db.Column(db.String(20)) # db.create_all() @app.route('/') def index(): return render_template('index.html') @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") user = User.query.filter(User.username == username).first() if user: return 'username existed.' else: user = User(username=username, password=password) db.session.add(user) #数据库,添加操作 db.session.commit() return redirect(url_for('login')) @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 == username).first() if user: if user.password == password: session['user'] = username return redirect(url_for('login')) else: return '密码错误' else: return '用户名不存在' if __name__ == '__main__': app.run(debug=True)
import os
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:@127.0.0.1:3306/mis1121?charset=utf8' SQLALCHEMY_TRACK_MODIFICATIONS = False SECRET_KEY = os.urandom(24)