完成登录功能,用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): __table__name = '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() @app.route('/') def index(): return render_template('index.html') @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('/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 return redirect(url_for('index')) else: return u'password error' else: return u'username is not existed' if __name__ == '__main__': app.run(debug=True)
import os SECRET_KEY = os.urandom(24) SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:@localhost:3306/mis_db?charset=utf8' SQLALCHEMY_TRACK_MODIFICATIONS = False
{% extends 'base.html' %} {% block title %}登录{% endblock %} {% block head %} <link href="{{ url_for('static',filename='css/login.css') }}" rel="stylesheet" type="text/css"> <script src="{{ url_for('static',filename='js/login.js') }}"></script> {% endblock %} <body background="http://img2.imgtn.bdimg.com/it/u=1662655920,2109868634&fm=200&gp=0.jpg"></body> {% block main %} <div class="aa" > <div class="login" ><h2>LOGIN</h2></div> <form action="{{ url_for('login') }}"method="post"> <div class="aa1" > Username:<input id="name" type="text"name='username'placeholder="请输入用户名"><br> Password:<input id="password" type="password"name='password'placeholder="请输入密码"><br> </div> <div id="error_box"><br></div> <div class="aa2" > <button onclick="myLogin()">Login</button> <button type="button" onclick=window.alert("是否取消登录!")>Cancel</button> <body background="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1510743700&di=99ef867fd6c5a1386163e38f9d7b5f2c&imgtype=jpg&er=1&src=http%3A%2F%2Fstatic.leiphone.com%2Fuploads%2Fnew%2Farticle%2Fpic%2F201610%2F57fb516e1b62a.jpg%3Fwatermark%2F1%2Fimage%2FaHR0cDovL20uaWdhbzcuY29tL3Jlc1dlYi9pbWFnZXMvc2h1aXlpbi5wbmc%3D%2Fdissolve%2F80%2Fgravity%2FSouth%2Fdx%2F20%2Fdy%2F20"style="background-repeat:no-repeat;background-size: 100% 100%; background-attachment: fixed;"></body> </div> </form> </div> {% endblock %}
function myLogin(){ var uName=document.getElementById("name"); var uError=document.getElementById("error_box"); var upass = document.getElementById("password"); var isError=true; uError.innerHTML = "<br>" //uname if(uName.value.length>12 || uName.value.length<6){ uError.innerHTML="Username 6-12 bits"; isError=false; return isError; }else if((uName.value.charCodeAt(0)>=48)&& uName.value.charCodeAt(0)<=57){ uError.innerHTML="The first letter cannot be numeric"; 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 = "only letter or number."; isNotError=false; return isError; } } if(upass.value.length>12 || upass.value.length<6){ uError.innerHTML="password 6-12 bits"; isError=false; return isError; } return isError; }
-