我是靠谱客的博主 无私金针菇,这篇文章主要介绍python flask 数据结构(七) 登陆,现在分享给大家,希望可以做个参考。

登录注册是一个网站、APP用户获取自主权限和能够进一步用户交互的开端,也是网页设计必不可缺的一环,如何将数据库,MVF,权限系统整合在一起进行使用呢?接下来我们将展开探讨。

其中主要有两个部分,html交互部分,flask MVF结构。

首先我们使用MVF模型结构的用户登录系统

html交互部分

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!doctype html> <html lang="zh"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <form action="/login" method="POST"> <input type="number" name="username" id="inputText" class="form-control" placeholder="Username" required autofocus> <input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required> <button type="submit">进入系统</button> </form> </body> </html>

    其中<form action="/login" method="POST">表示通过POST的方式传参进入/login路由

    两个input的name为获取placeholder的标志

flask操作指令(MVF结构):

forms.py结构

复制代码
1
2
3
4
5
6
7
from flask_wtf import FlaskForm from wtforms import StringField, PasswordField, SubmitField from wtforms.validators import DataRequired class LoginForm(FlaskForm): username = StringField('Username', validators=[DataRequired()]) password = PasswordField('Password', validators=[DataRequired()]) submit = SubmitField('Sign In')

设计的登录功能栏

models.py结构

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from werkzeug.security import generate_password_hash, check_password_hash from flask_login import UserMixin from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config["SQLALCHEMY_DATABASE_URI"] = 'mysql+pymysql://{0}:{1}@{2}/{3}?charset=utf8' app.config["SQLALCHEMY_TRACK_MODIFICATION"] = True app.config["SECRET_KEY"]="ZHANGSan" app.debug = True db = SQLAlchemy(app) class User_info(UserMixin, db.Model): # 管理员 ID = db.Column(db.String(8), primary_key=True) Username = db.Column(db.String(10), nullable=False) UserPassword = db.Column(db.Text, nullable=False) # override def get_id(self): return self.ID def set_password(self, password): self.UserPassword = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.UserPassword, password)

设计的登录数据库以及数据操作功能,以上,其中{0}为数据库账号,{1}为数据库密码,{2}为数据库地址,{3}为数据库的库名(是库名不是表名)

view.py

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
from app.models import User_info @app.route('/login', methods=('GET', 'POST')) def login(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] user = User_info.query.filter_by(Username=username).first() if user is None or not user.check_password(password): flash('Invalid username or password!') return redirect(url_for('login')) login_user(user) return redirect(url_for('main')) return render_template('login.html')

设计登录路由视图'/login',首先获取login.html中的username和password再通过User_info的数据操作鉴别账号密码,如果通过,则转跳main路由页面

最后

以上就是无私金针菇最近收集整理的关于python flask 数据结构(七) 登陆的全部内容,更多相关python内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(111)

评论列表共有 0 条评论

立即
投稿
返回
顶部