完成登录功能,用session记住用户名
登录功能完成:
- js:设置return
- html:设置
- form
- input
-
onclick="return fnLogin()"
- py:
- @app.route设置methods
- GET
- POST
- 读取表单数据
- 查询数据库
- 用户名密码对:
- 记住用户名
- 跳转到首页
- 用户名密码不对:
- 提示相应错误。
- 用户名密码对:
session:
- 从`flask`中导入`session`
- 设置`SECRET_KEY`
- 操作字典一样操作`session`:增加用户名`session['username']=`username
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) nickname = db.Column(db.String(20)) #db.create_all() @app.route('/') def vi(): return render_template("base.html") @app.route('/first') def base(): return render_template("base.html") @app.route("/login",methods=["GET","POST"]) def login(): if request.method == "GET": return render_template("login.html") else: usern = request.form.get("usern") passw = request.form.get("userp") user = User.query.filter(User.username==usern).first() if user: if user.password == passw: session["user"] = usern # session.permanent = True return redirect(url_for("base")) else: return "密码错误,请重新输入!" else: return "用户名不存在!" @app.route("/regist",methods=["GET","POST"]) def regist(): if request.method == "GET": return render_template("regist.html") else: usern1 = request.form.get("newn") passw1 = request.form.get("newp") user = User.query.filter(User.username==usern1).first() if user: return "用户名已存在!" else: user = User(username = usern1,password = passw1) db.session.add(user) db.session.commit() return redirect(url_for("login")) 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 'base.html' %} {% block title %} 登录 {% endblock %} {% block head %} <link href="{{ url_for("static",filename="css/vv.css") }}" rel="stylesheet" type="text/css"> <script src="{{ url_for("static",filename="js/login.js") }}"></script> {% endblock %} {% block main %} <body bgcolor="#778899" class="div4"> <div class="div1"> <h2 class="title"></h2> <form action="{{ url_for("login") }}"method="post"> <div class="normal-title"> <a class="div2" href="login.html">登录</a> <b>|</b> <a class="div2" href="regist.html">注册</a> </div> <div class="div3"> 用户:<input id="username" type="text" placeholder="请输入昵称" name="usern"> </div> <div class="div3"> 密码:<input id="userpass" type="text" placeholder="请输入密码" name="userp"> </div> <div id="error_box"><br></div> <div class="div3"> <button onclick="return myLogin()">Login</button> </div> <div class="div2">ByYin</div> </form> </div> <br> </body> {% endblock %} </html>
function myLogin() { var uName = document.getElementById("username"); var uError = document.getElementById("error_box"); var uPass =document.getElementById("userpass"); var isError = true; uError.innerHTML="<br>"; if (uName.value.length < 6||uName.value.length > 20) { uError.innerHTML = "用户名为6位到20位!"; isError = false; return isError; } else if ((uName.value.charCodeAt(0)) >= 48 && uName.value.charCodeAt(0) <= 57) { uError.innerHTML = "用户名首字母不能为数字"; isError = false; return isError; } 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="用户名只能为字母或数字"; isError = false; return isError; } } if (uPass.value.length<6||uPass.value.length>20){ uError.innerHTML="密码为6-20位!"; isError = false; return isError; } return isError; }