至此,一个简单的由前端向后端的form表单增删查改(基础)方式已经完成
# 用户删除
@bp.route('/delete', methods=['GET', 'POST'])
def delete():
if request.method == 'GET':
return render_template('delete.html')
else:
form = DeleteForm(request.form)
if form.validate():
username = form.username.data
user = Usermodel.query.filter_by(username=username).first()
if not user:
print('User not found')
return redirect(url_for('auth.delete'))
else:
Usermodel.query.filter_by(username=form.username.data).delete()
db.session.commit()
return "用户已删除完毕"
# 邮箱修改
@bp.route('update', methods=['GET', 'POST'])
def update():
if request.method == 'GET':
return render_template('update.html')
else:
form = UpdateForm(request.form)
if form.validate():
username = form.username.data
user = Usermodel.query.filter_by(username=username).first()
if not user:
print("用户在数据库中不存在")
return redirect(url_for('auth.update'))
else:
Usermodel.query.filter_by(username=form.username.data).update({'email': form.email.data})
db.session.commit()
return redirect(url_for('/'))
# 登录界面及密码验证
@bp.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'GET':
return render_template('login.html')
else:
form = LoginForm(request.form)
if form.validate():
username = form.username.data
password = form.password.data
user = Usermodel.query.filter_by(username=username).first()
if not user:
print("用户在数据库中不存在!")
return redirect(url_for('auth.login'))
if check_password_hash(user.password, password):
# cookie:只能存储少量东西--通过cookie识别,通常存放登录授权的东西
# flask中的session,是通过加密后存储在cookie中的
session['user_id'] = user.id
return redirect('/')
else:
print('密码错误!')
return redirect(url_for('auth.login'))
else:
print(form.errors)
return redirect(url_for('auth.login'))
# 表单验证:flask-wtf
@bp.route('/myregister', methods=['GET', 'POST'])
def register():
if request.method == 'GET':
return render_template('myregist.html')
else:
# 表单验证:flask-wtf: wtforms
form = RegistrationForm(request.form)
if form.validate():
username = form.username.data
password = form.password.data
email = form.email.data
user = Usermodel(username=username, password=generate_password_hash(password), email=email)
db.session.add(user)
db.session.commit()
return redirect(url_for('auth.login'))
else:
print(form.errors)
return redirect(url_for('auth.register'))
这是这段时间学习的一个小的记录,其中flask用到的方法有:
Blueprint, render_template, request, redirect, url_for, session
.form中有:
RegistrationForm, LoginForm, UpdateForm, DeleteForm(这些是我自己在form中创建的类,其实可以不用那么麻烦,因为存在重复的内容,存在“尸体”代码)
from werkzeug.security import generate_password_hash, check_password_hashh
还导入了该方法对密码进行加密 ------ 以下是该author.py导入的库
from flask import Blueprint, render_template, request, redirect, url_for, session
from sqlalchemy.testing.pickleable import User
from exts import mail, db
from flask_mail import Message
from .forms import RegistrationForm, LoginForm, UpdateForm, DeleteForm
from models import Usermodel
from werkzeug.security import generate_password_hash, check_password_hash
# 这里的url_prefix代表的蓝图前缀,以后访问就是以/auth/...来访问
bp = Blueprint('auth', __name__, url_prefix='/auth')