登录功能完成:
1.js:设置return
2.html:设置
- form
- input
- onclick="return fnLogin()"
3.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) db.create_all() @app.route('/') 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: 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 session.permanent = True return redirect(url_for('base')) else: return 'Password error' else: return 'Username inexistent' @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: user1 = User(username=username, password=password) db.session.add(user1) db.session.commit() return redirect(url_for('login')) @app.route('/question/') def question(): return render_template('question.html') if __name__ == '__main__': app.run(debug=True)
import os SECRET_KEY = os.urandom(24) SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:@localhost:3306/wuwen?charset=utf8' SQLALCHEMY_TRACK_MODIFICATIONS = False
{% extends 'base.html' %} {% block title %}Login{% endblock %} {% block head %} <link href="{{ url_for('static',filename='CSS/login.css') }}" rel="stylesheet" type="text/css"> <script src="{{ url_for('static',filename='JScript/login.js') }}"></script> {% endblock %} {% block main %} <div class="login"> <form action="{{ url_for('login') }}" method="post"> <div class="login-top"> <h1>LOGIN FORM</h1> <input id="uname" name="username" type="text" value="Username" onfocus="this.value = '';" onblur="if (this.value==''){ this.value = 'Username';}"> <input id="upass" name="password" type="password" value="Password" onfocus="this.value = '';" onblur="if (this.value==''){ this.value = 'Password';}"> <div class="forgot"> <input type="checkbox"><p>Keep me logged in</p> <a href="#">forgot Password</a> </div> </div> <div class="login-bottom"> <div id="errorbox"></div><button onclick="return MyLogin()">Login</button> </div> </form> </div> {% endblock %}
function MyLogin() { var oUname=document.getElementById("uname"); var oUpass=document.getElementById("upass"); var oError=document.getElementById("errorbox"); //username if(oUname.value=="Username"&&oUpass.value=="Password"){ oError.innerHTML="Please input username"; return false; }else if(oUname.value.length<6||oUname.value.length>12){ oError.innerHTML="Username must be 6 to 12"; return false; }else if(oUname.value.charCodeAt(0)>=48 && oUname.value.charCodeAt(0)<=57){ oError.innerHTML="Frist Letter"; 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 = "Only letter or number"; return false; } } //password if(oUpass.value.length<6||oUpass.value.length>12) { oError.innerHTML = "Password must be 6 to 12"; return false; } return true; // window.alert("Login Successful") }