完成登录功能,用session记住用户名v
登录功能完成:
- js:设置return
- html:设置
- form
- input
- py:
- @app.route设置methods
- GET
- POST
- 读取表单数据
- 查询数据库
- 用户名密码对:
- 记住用户名
- 跳转到首页
- 用户名密码不对:
- 提示相应错误。
- 用户名密码对:
session:
- 从`flask`中导入`session`
- 设置`SECRET_KEY`
- 操作字典一样操作`session`:增加用户名`session['username']=`username
function foLogin() { var oUname = document.getElementById("uname"); var oError = document.getElementById("error_box"); var oUpass = document.getElementById("upass"); var isError = true; oError.innerHTML = "<br>"; if (oUname.value.length < 6 || oUname.value.length > 12) { oError.innerHTML = "用户名要6-12位"; isError = false; return; }else if(oUname.value.charCodeAt(0)>=48 &&(oUname.value.charCodeAt(0)<=57)){ oError.innerHTML="首位不能为数字"; isError = false; 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)<58)&&(oUname.value.charCodeAt(i)>97)){ oError.innerHTML="只能为数字和字母"; isError = false; return isError; } } if (oUpass.value.length < 6 || oUpass.value.length > 12) { oError.innerHTML = "密码要6-20位"; isError = false; isError = false; return isError; } return ture; }
{% extends'index.html' %} {% block title %}登录 {% endblock %} {% block head %} <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/886.css') }}"> <script src="{{ url_for('static',filename='js/tt.js')}}"></script> {% endblock %} {% block main %} <div class="box"> <h2 class="dd">登录</h2> <form action="{{url_for('login')}}" method="post"> <div class="input_box"> <input id="uname" type="text" placeholder="请输入用户名" name="username"> </div> <div class="input_box"> <input id="upass" type="password" placeholder="请输入密码" name="password"> </div> <div id="error_box"><br></div> <div> <button class="jj" onclick="fnLogin()">登录</button> </div> </form> </div> {% endblock %}
from flask import Flask,render_template,request,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=True,autoincrement=True) username = db.Column(db.String(20),nullable=False) password = db.Column(db.String(20),nullable=False) nickname = db.Column(db.String(50)) # db.create_all() # 增加 # user = User(username='mis1114',password = 'mis1114',nickname='mis1114') # db.session.add(user) # db.session.commit() # # 查询 # User.query.filter(User.username == 'mis1114').first() # #修改 # user =User.query.filter(User.username =='mis1114').first() # user.password='23456' # print(user.username,user.password) # db.session.commit() # # 删除 # user =User.query.filter(User.username =='mis1114').first() # db.session.delete(user) # db.session.commit() @app.route('/') def index(): return render_template('index.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,User.password==password).first() if user: session['user']=username session.permanent=True return redirect(url_for('index')) else: return u'error username or password' @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 u'username existed' else: user = User(username=username, password= password, nickname=nickname) db.session.add(user) 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)