登录功能完成:
- js:设置return
regist.js
function fnRegist() { var zcoUname = document.getElementById("zcuname") var zcoError = document.getElementById("zcerror_box") var zcoUword1 = document.getElementById("zcuword1") var zcoUword2 = document.getElementById("zcuword2") zcoError.innerHTML = "<br>" if (zcoUname.value.length < 6 || zcoUname.value.length > 12) { zcoError.innerHTML = "用户名为6到12位"; return false; } else if ((zcoUname.value.charCodeAt(0) >= 48) && (zcoUname.value.charCodeAt(0) <= 57)) { zcoError.innerHTML = "用户名首位不能是数字"; return false; } else for (var i = 0; i < zcoUname.value.length; i++) { if ((zcoUname.value.charCodeAt(i) < 48) || (zcoUname.value.charCodeAt(i) > 57) && (zcoUname.value.charCodeAt(i) < 97) || (zcoUname.value.charCodeAt(i) > 122)) { zcoError.innerHTML = "用户名只能是字母与数字"; return false; } } if ((zcoUword1.value.length < 6) || (zcoUword1.value.length > 20)) { zcoError.innerHTML = "密码为6到20位"; return false; } else if (zcoUword2.value.length == 0) { zcoError.innerHTML = "请再次输入密码"; return false; } else if (zcoUword1.value != zcoUword2.value) { zcoError.innerHTML = "两次密码不一致" return false; } return true; }
login.js
function fnLogin() { var oUname = document.getElementById("uname") var oError = document.getElementById("error_box") var oUword = document.getElementById("uword") oError.innerHTML = "<br>" if (oUname.value.length < 6 || oUname.value.length > 12) { oError.innerHTML = "用户名为6到12位"; 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; } } if ((oUword.value.length < 6) || (oUword.value.length > 20)) { oError.innerHTML = "密码为6到20位"; return false; } return true; }
- html:设置
- form
- input
<form action="{{ url_for('regist') }}" method="post" onsubmit="return fnRegist()">
......
<div class="zcinput_box">
<input type="submit" value="立即注册"></input>
</div>
</form>
- py:
- @app.route设置methods
- GET
- POST
- 读取表单数据
- 查询数据库
- 用户名密码对:
- 记住用户名
- 跳转到首页
- 用户名密码不对:
- 提示相应错误。
- 用户名密码对:
@app.route('/login/', methods=['GET', 'POST']) def login(): if request.method=='GET': return render_template('login.html') else: usern = request.form.get('username') passw = request.form.get('password') user = User.query.filter(User.username == usern,User.password==passw).first() if user: session['user']=usern session.permanent=True return redirect(url_for('index')) else: return u'用户名不存在/密码错误' @app.route('/regist/', methods=['GET', 'POST']) def regist(): if request.method == 'GET': return render_template('regist.html') else: # 获取form中的数据,判断用户名是否存在,存到数据库中 username = request.form.get('username') nickname = request.form.get('nickname') password = request.form.get('password') user = User.query.filter(User.username == username).first() # 重定向到登录页 if user: return u'用户名已存在' else: user = User(username=username, nickname=nickname, password=password) db.session.add(user) db.session.commit() return redirect(url_for('login'))
session:
- 从`flask`中导入`session`
rom flask import Flask, render_template, request, redirect, url_for,session
- 设置`SECRET_KEY`
import os DEBUG=True SECRET_KEY=os.urandom(24) DIALEXT='mysql' DRIVER='mysqldb' USERNAME='root' PASSWORD='ROOT' HOST='@localhost' PORT='3306' DATABASE='meow'
- 操作字典一样操作`session`:增加用户名`session['username']=`username