完成登录功能,用session记住用户名
1.登录功能完成:
- js:设置return
- html:设置
- form
- input
- py:
- @app.route设置methods
- GET
- POST
- 读取表单数据
- 查询数据库
- 用户名密码对:
- 记住用户名
- 跳转到首页
- 用户名密码不对:
- 提示相应错误。
- 用户名密码对:
session:
- 从`flask`中导入`session`
- 设置`SECRET_KEY`
- 操作字典一样操作`session`:增加用户名`session['username']=`username
login.html
<!DOCTYPE html> <html lang="en"> <head> <script type="text/javascript" src="../static/js/login.js"> </script> <meta charset="UTF-8"> <title></title> <link rel="stylesheet" type="text/css" href="../static/css/style.css"> <link href='http://fonts.googleapis.com/css?family=Oleo+Script' rel='stylesheet' type='text/css'> </head> <body> <div class="lg-container" > <h1>登录</h1> <form action="{{ url_for('login') }}" id="lg-form" name="lg-form" method="post"> <div> <label for="username">Username:</label> <input id="uname" type="text" name="用户名" placeholder="请输入用户名"><br> </div> <div> <label for="password">Password:</label> <input class="password" id="upass" type="password" name="密码" placeholder="请输入密码" > </div> <input type="checkbox" name="vehicle" value="true"><span>记住密码</span> <a class="right" href="">登录遇到问题?</a> <div id="error_box"></div> <button type="submit" value="登录" id="login" onclick="return fnlogin()">登录</button> </form> <div id="message"></div> </div> </body> </html>
login.js
ar oUname = document.getElementById("uname"); var oUpass = document.getElementById("upass"); var oUpass2 = document.getElementById("upass2"); var oError = document.getElementById("error_box"); var isError = true; oError.innerHTML = "<br>"; if (oUname.value.length < 6 || oUname.value.length > 12) { oError.innerHTML = "用户名6-12位"; isError = false; // noinspection JSAnnotator return isError; } else if ((oUname.value.charCodeAt(0) >= 48) && (oUname.value.charCodeAt(0) <= 57)) { oError.innerHTML = "first letter"; isError = false; // noinspection JSAnnotator 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 = "only letter or number"; isError = false; // noinspection JSAnnotator return isError; } }
myqa.py
from flask import Flask, request, render_template,redirect,url_for,session from flask_sqlalchemy import SQLAlchemy import config # from functools import wraps # from datetime import datetime app = Flask(__name__) app.config.from_object(config) db=SQLAlchemy(app) class User(db.Model): __tablename__='user' id=db.Column(db.Integer,primary_key=TabError,autoincrement=True) username=db.Column(db.String(20),nullable=False) password=db.Column(db.String(20),nullable=False) nickname=db.Column(db.String(20)) # user = User(username ='vixx',password='20120524' ) # db.session.add(user) # db.session.commit() # db.create_all() @app.route('/') def index(): return render_template('homepage.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==usern).first() if user: session['user']=usern session.permanent=True if user.password==passw: return redirect(url_for('homepage')) else: return u'password error' else: return u'username is not existed' @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') nickname = request.form.get('nickname') user =User.query.filter(User.username==username).first() if user: return 'username existed' else: user=User(username=username,password=password,nickname=nickname) db.session.add(user) #数据库操作 db.session.commit() return redirect(url_for('login')) if __name__ == '__main__': app.run()