完成登录功能,用session记住用户名
Posted 蒋宇翔
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了完成登录功能,用session记住用户名相关的知识,希望对你有一定的参考价值。
登录功能完成:
- js:设置return
- html:设置
- form
- input
- py:
- @app.route设置methods
- GET
- POST
- 读取表单数据
- 查询数据库
- 用户名密码对:
- 记住用户名
- 跳转到首页
- 用户名密码不对:
- 提示相应错误。
- 用户名密码对:
session:
- 从`flask`中导入`session`
- 设置`SECRET_KEY`
- 操作字典一样操作`session`:增加用户名`session[‘username‘]=`username
function fnLogin() { var uSer = document.getElementById("user"); var pAss = document.getElementById("pass"); var oError = document.getElementById("error_box"); oError.innerHTML = "<br>"; if (uSer.value.length < 6 || uSer.value.length > 20) { oError.innerHTML = "请输入6-20位用户名"; return } else if ((uSer.value.charCodeAt(0) >= 48) && (uSer.value.charCodeAt(0) <= 57)) { oError.innerHTML = "用户名首字不能是数字"; return } else for (var i = 0; i < uSer.value.length; i++) { if ((uSer.value.charCodeAt(i) < 48) || (uSer.value.charCodeAt(i) > 57) && (uSer.value.charCodeAt(i) < 97) || (uSer.value.charCodeAt(i) > 122)) { oError.innerHTML = "用户名只能是数字和字母"; return } } if (pAss.value.length < 6 || pAss.value.length > 20) { oError.innerHTML = "请输入6-20位密码"; return } // 验证弹框 window.alert("成功") } function fnRegistration() { var uSer = document.getElementById("user"); var pAss = document.getElementById("pass"); var aGain = document.getElementById("again"); var oError = document.getElementById("error_box"); oError.innerHTML = "<br>"; // 验证用户名 if (uSer.value.length < 6 || uSer.value.length > 20) { oError.innerHTML = "请输入6-20位用户名"; return } else if ((uSer.value.charCodeAt(0) >= 48) && (uSer.value.charCodeAt(0) <= 57)) { oError.innerHTML = "用户名首字不能是数字"; return } else for (var i = 0; i < uSer.value.length; i++) { if ((uSer.value.charCodeAt(i) < 48) || (uSer.value.charCodeAt(i) > 57) && (uSer.value.charCodeAt(i) < 97) || (uSer.value.charCodeAt(i) > 122)) { oError.innerHTML = "用户名只能是数字和字母"; return } } // 验证密码 if (pAss.value.length < 6 || pAss.value.length > 20) { oError.innerHTML = "请输入6-20位密码"; return } // 验证再次输入的密码 if (aGain.value != pAss.value) { oError.innerHTML = "请输入相同的密码"; return } // 验证弹框 window.alert("成功") }
PY:
from flask import Flask, render_template, url_for, redirect, request 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() # 增加 # user = User(username=‘tan1997‘,password=‘19961021‘) # db.session.add(user) # db.session.commit() # 查询 # user = User.query.filter(User.username == ‘tan1997‘).first() # print(user.username,user.password) # 修改 # user=User.query.filter(User.username == ‘tan1997‘).first() # user.password=1234567 # db.session.commit() # 删除 # user=User.query.filter(User.username == ‘tan1997‘).first() # db.session.delete(user) # db.session.commit() @app.route(‘/‘) def myweb(): return render_template("denglu.html") @app.route(‘/login/‘, methods=[‘GET‘, ‘POST‘]) def login(): if request.method == ‘GET‘: return render_template("zhuce.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: return redirect(url_for(‘myweb‘)) else: return ‘错误‘ else: return ‘用户不存在‘ @app.route(‘/regist/‘, methods=[‘GET‘, ‘POST‘]) def regist(): if request.method == ‘GET‘: return render_template("zhuce.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 ‘用户已存在‘ 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)
HTML:
{% extends ‘daohang.html‘ %} {% block denglutitle %}登陆{% endblock %} {% block dengluhead %} <link rel="stylesheet" type="text/css" href="{{ url_for(‘static‘,filename=‘css/denglu.css‘) }}"> <script src="{{ url_for(‘static‘,filename=‘js/text.js‘) }}"></script> {% endblock %} {% block denglubody %} <div id="container"> <div id="header"><h2 align="center">登录</h2></div> <div id="content"> <form> 账号:<input type="text" name="user" id="user" placeholder="输入用户名"> <br> 密码:<input type="password" name="pass" id="pass" placeholder="输入密码"> <input type="checkbox" name="c1" id="c1" value="">记住账号 <br> <div id="error_box"><br></div> <input type="button" value="登录" onclick="fnLogin()">  </form> </div></div> {% endblock %}
import os DEBUG = True SECRET_KEY = os.urandom(24) DIALECT = ‘mysql‘ DRIVER = ‘mysqldb‘ USERNAME = ‘root‘ PASSWORD = ‘ROOT‘ HOST = ‘127.0.0.1‘ DATABASE = ‘mytest‘ SQLALCHEMY_DATABASE_URI = ‘mysql+pymysql://root:@127.0.0.1:3306/mytest?charset=utf8‘ SQLALCHEMY_TRACK_MODIFICATIONS = False
以上是关于完成登录功能,用session记住用户名的主要内容,如果未能解决你的问题,请参考以下文章