完成登录功能,用session记住用户名
Posted 杜丽晖
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了完成登录功能,用session记住用户名相关的知识,希望对你有一定的参考价值。
登录功能完成:
- js:设置return
- html:设置
- form
- input
-
onclick="return fnLogin()"
- py:
- @app.route设置methods
- GET
- POST
- 读取表单数据
- 查询数据库
- 用户名密码对:
- 记住用户名
- 跳转到首页
- 用户名密码不对:
- 提示相应错误。
- 用户名密码对:
session:
- 从`flask`中导入`session`
- 设置`SECRET_KEY`
- 操作字典一样操作`session`:增加用户名`session[‘username‘]=`username
import os SQLALCHEMY_DATABASE_URI = ‘mysql+pymysql://root:@127.0.0.1:3306/niub1?charset=utf8‘ SQLALCHEMY_TRACK_MODIFICATIONS = False SECRET_KEY = os.urandom(24)
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() #db.create_all() # #增加 # user = User(username=‘l123123‘,password = ‘666666‘) # db.session.add(user) # db.session.commit() # # #查询 # user =User.query.filter(User.username ==‘a321321‘).first() # print(user.username,user.password) # # #修改 # user =User.query.filter(User.username ==‘l123123‘).first() # user.password=‘66666666‘ # print(user.username,user.password) # db.session.commit() # # #删除 # user =User.query.filter(User.username ==‘l123123‘).first() # print(user.username,user.password) # db.session.delete(user) # db.session.commit() @app.route(‘/‘) def base(): return render_template(‘base.html‘) @app.route(‘/base2‘) def base2(): return render_template(‘base2.html‘) @app.route(‘/login/‘,methods=[‘GET‘,‘POST‘]) def login(): if request.method ==‘GET‘: return render_template(‘login.html‘) else: usern = request.form.get(‘username‘) passw = request.form.get(‘password‘) user = User.query.filter(User.username == usern).first() if user: if user.password == passw: session[‘user‘]=usern return redirect(url_for("base2")) else: return u‘用户密码错误!‘ else: return u‘用户不存在!‘ @app.route(‘/res/‘,methods=[‘GET‘,‘POST‘]) def res(): if request.method ==‘GET‘: return render_template(‘res.html‘) else: usern = request.form.get(‘name‘) passw = request.form.get(‘password‘) user = User.query.filter(User.username==usern).first() if user: return ‘username existed‘ else: user= User(username=usern,password=passw) db.session.add(user) db.session.commit() return redirect(url_for(‘login‘)) if __name__ == ‘__main__‘: app.run(debug=True)
{% extends ‘base.html‘ %} {% block title %}LOGIN{% endblock %} {% block head %} <link rel="stylesheet" type="text/css" href="../static/css/login.css"> <script src="../static/js/login.js"></script> {% endblock %} {% block main %} <body class="bg"> <div class="box" > <form action="{{url_for(‘login‘)}}" method="post"> <div class="lg1" ><h2>? 欢迎登录 ?</h2></div> <div class="box2" > 姓名:<input id="name" type="text" placeholder="请输入用户名" name="username"><br> 密码:<input id="password" type="password" placeholder="请输入密码" name="password"><br> </div> <div id="error_box"><br></div> <form class="lg2" > <button type="submit" onclick="myLogin()">登录</button> <button type="submit" onclick=window.alert("是否取消登录!")>取消</button> <br> <div class="design"> <div class="lg3" ></div> <p>Design by Doublewhere</p> </div> </form></form>> </div> </div> </body> {% endblock %}
以上是关于完成登录功能,用session记住用户名的主要内容,如果未能解决你的问题,请参考以下文章