完成登录功能,用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
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() #增加 # user = User(username =‘daili‘,password=‘12345‘ ) # db.session.add(user) # db.session.commit() #查询 # user=User.query.filter(User.username ==‘vixx‘).first() # print(user.username,user.password) #删除 # user=User.query.filter(User.username ==‘vixx‘).first() # db.session.delete(user) # db.session.commit() # #修改 # user=User.query.filter(User.username ==‘vixx‘).first() # user.password=‘123456789‘ # db.session.commit() @app.route(‘/‘) def base(): return render_template(‘base.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.passw==passw: session[‘user‘]=usern session.permanent=True return redirect(url_for(‘base‘)) else: return u‘error usern or passw‘ # # return render_template(‘login.html‘) @app.route(‘/regist/‘, methods=[‘GET‘,‘POST‘]) def regist(): if request.method==‘GET‘: return render_template(‘regist.html‘) else: username=request.form.get(‘username‘) print(username) password= request.form.get(‘password‘) user=User.query.filter(User.username==username).first() if user: return ‘username existed‘ else: user=User(username=username,password=password) db.session.add(user) db.session.commit() return redirect(url_for(‘login‘)) @app.route(‘/zimoban/‘) def zimoban(): return render_template(‘zimoban.html‘) @app.route(‘/zzimoban/‘) def zzimoban(): return render_template(‘zzimoban.html‘) @app.route(‘/index/‘) def index(): return render_template(‘index.html‘) if __name__ == ‘__main__‘: app.run(debug=True)
{% extends ‘base.html‘ %} {% block title %}登录{% endblock %} {% block head %} <link rel="stylesheet" type="text/css" href="../static/css/20.css"> <script src="../static/JS/JS30.js"></script> <script src="{{ url_for(‘static‘,filename=‘JS/JS30.js‘) }}"></script> {% endblock %} {% block main %} <body style="background: burlywood"> <div class="box"> <h1>登录</h1> <form action="{{ url_for(‘login‘) }}" method="post"> <div class="input_box"> 帐号:<input id="name" type="text" placeholder="请输入用户名" name="username"> </div> <div class="input_box"> 密码:<input id="pass" type="password" placeholder="请输入密码" name="password"> </div> <div id="error_box"><br> </div> <div class="input_box"> <button onclick="fnLogin()">login</button> <a class="link-forget cl-link-blue" href="get_password.html">忘记密码</a><br> <a href="regist.html"><input type="button" class="btn btn-info" name="regist" value="新用户注册"></a> </div></form> </div> </body> {% endblock %}
import os SECRET_KEY=os.urandom(24) SQLALCHEMY_DATABASE_URI=‘mysql+pymysql://root:@127.0.0.1:3306/test?charset=utf8‘ SQLALCHEMY_TRACK_MODIFICATTONS = False
以上是关于完成登录功能,用session记住用户名的主要内容,如果未能解决你的问题,请参考以下文章