1.用户信息_初始化
1.1使用VSCode执行sql代码
删除原来的表,新添加一行gener,生成表后插入数据
sql代码已经写好了,直接运行即可
INSERT INTO `t_user` (`id`, `create_time`, `email`, `nick_name`, `password`, `gender`, `signatrue`, `pic`, `status`) VALUES ('5ddf83c8b78aa66cfb5b025e', '2050-01-01 00:00:00', '1329372653@', 'gaofei', '202cb962ac59075b964b07152d234b70', 0, 'Hello,记录美好生活!', '/static/img/1575015920996-2.jpg', 0);INSERT INTO `t_user` (`id`, `create_time`, `email`, `nick_name`, `password`, `gender`, `signatrue`, `pic`, `status`) VALUES ('5de0b487b07c402cc7d1a00f', '2050-01-01 00:00:00', '912455894@', '啦啦啦啦', '202cb962ac59075b964b07152d234b70', 1, 'Hello,记录美好生活!', '/static/img/1575016093531-3.jpg', 0);INSERT INTO `t_user` (`id`, `create_time`, `email`, `nick_name`, `password`, `gender`, `signatrue`, `pic`, `status`) VALUES ('5de0d2696e78ad79c2e2c64a', '2050-01-01 00:00:00', '1@', '寂寞ゝ专属.', '202cb962ac59075b964b07152d234b70', 0, 'Hello,记录美好生活!', '/static/img/1575015590957-1.jpg', 0);INSERT INTO `t_user` (`id`, `create_time`, `email`, `nick_name`, `password`, `gender`, `signatrue`, `pic`, `status`) VALUES ('5de0d74a5dd0a380dbc1aca7', '2050-01-01 00:00:00', '2@', 'ー£゛走了ノ没留下什么', '202cb962ac59075b964b07152d234b70', 0, 'Hello,记录美好生活!', '/static/img/1575016293522-4.jpg', 0);INSERT INTO `t_user` (`id`, `create_time`, `email`, `nick_name`, `password`, `gender`, `signatrue`, `pic`, `status`) VALUES ('5de0d8745dd0a380dbc1aca9', '2050-01-01 00:00:00', '3@', '言晚', '202cb962ac59075b964b07152d234b70', 0, 'Hello,记录美好生活!', '/static/img/1575016586255-5.jpg', 0);INSERT INTO `t_user` (`id`, `create_time`, `email`, `nick_name`, `password`, `gender`, `signatrue`, `pic`, `status`) VALUES ('5de0d9375dd0a380dbc1acab', '2050-01-01 00:00:00', '4@', '人生如戏', '202cb962ac59075b964b07152d234b70', 0, 'Hello,记录美好生活!', '/static/img/1575016781746-6.jpg', 0);INSERT INTO `t_user` (`id`, `create_time`, `email`, `nick_name`, `password`, `gender`, `signatrue`, `pic`, `status`) VALUES ('5de0d9d65dd0a380dbc1acad', '2050-01-01 00:00:00', '5@', '香了一口※', '202cb962ac59075b964b07152d234b70', 0, 'Hello,记录美好生活!', '/static/img/1575016947448-下载 (5).jpeg', 0);INSERT INTO `t_user` (`id`, `create_time`, `email`, `nick_name`, `password`, `gender`, `signatrue`, `pic`, `status`) VALUES ('5de0da8e5dd0a380dbc1acaf', '2050-01-01 00:00:00', '6@', '丶花少', '202cb962ac59075b964b07152d234b70', 0, 'Hello,记录美好生活!', '/static/img/1575017124098-下载 (4).jpeg', 0);INSERT INTO `t_user` (`id`, `create_time`, `email`, `nick_name`, `password`, `gender`, `signatrue`, `pic`, `status`) VALUES ('5de0db705dd0a380dbc1acb2', '2050-01-01 00:00:00', '7@', 'Bitter 祭末', '202cb962ac59075b964b07152d234b70', 0, 'Hello,记录美好生活!', '/static/img/1575017346967-下载 (3).jpeg', 0);INSERT INTO `t_user` (`id`, `create_time`, `email`, `nick_name`, `password`, `gender`, `signatrue`, `pic`, `status`) VALUES ('5de0dc775dd0a380dbc1acb4', '2050-01-01 00:00:00', '8@', '妳愛我嗎?', '202cb962ac59075b964b07152d234b70', 0, 'Hello,记录美好生活!', '/static/img/1575017609800-下载 (2).jpeg', 0);INSERT INTO `t_user` (`id`, `create_time`, `email`, `nick_name`, `password`, `gender`, `signatrue`, `pic`, `status`) VALUES ('5de0dd345dd0a380dbc1acb6', '2050-01-01 00:00:00', '9@', '少女病`', '202cb962ac59075b964b07152d234b70', 0, 'Hello,记录美好生活!', '/static/img/1575017800873-7.jpeg', 0);INSERT INTO `t_user` (`id`, `create_time`, `email`, `nick_name`, `password`, `gender`, `signatrue`, `pic`, `status`) VALUES ('5de0ddaa5dd0a380dbc1acb8', '2050-01-01 00:00:00', '10@', '王思聪', '202cb962ac59075b964b07152d234b70', 0, 'Hello,记录美好生活!', '/static/img/1575017936343-7c1ed21b0ef41bd5a7f81d995ada81cb39db3d37.png', 0);INSERT INTO `t_user` (`id`, `create_time`, `email`, `nick_name`, `password`, `gender`, `signatrue`, `pic`, `status`) VALUES ('5de0de4f5dd0a380dbc1acba', '2050-01-01 00:00:00', '11@', '殃国娇颜', '202cb962ac59075b964b07152d234b70', 0, 'Hello,记录美好生活!', '/static/img/1575018084252-下载 (1).jpeg', 0);INSERT INTO `t_user` (`id`, `create_time`, `email`, `nick_name`, `password`, `gender`, `signatrue`, `pic`, `status`) VALUES ('5e4a27910cb2afe7559ae7d9', '2050-01-01 00:00:00', '110@', '110', '202cb962ac59075b964b07152d234b7078', 0, 'Hello,记录美好生活!', '/static/img/1581918166606-avatar.jpg', 0);
1.2登录效果展示
2.用户信息_更新信息
2.1观察前端需要的接口与传递的数据
分析可得,已经传递了用户所有得信息,并且api接口时api/user/update
2.2开发接口
WTForms表单验证
# forum/wtforms.py# 完整注册(给t_user表)增加数据之前的数据验证from wtforms_tornado import Formfrom wtforms.fields import StringFieldfrom wtforms.fields.simple import HiddenFieldfrom wtforms.validators import DataRequired, Length# 对更新的数据验证class UpdateUserForm(Form):id = HiddenField()email = StringField('账号', validators=[DataRequired(message='请填写合法的邮箱地址'),Length(min=5, max=20, message='请输入5-20长度的邮箱')])nick_name = StringField('昵称',validators=[Length(min=2, max=10, message='请输入2-10长度的昵称')])gender = StringField('性别')signature = StringField('签名')pic = StringField('头像')
接口编写
# forum/handler/UserHandler.pyfrom uuid import uuid4from random import randintimport jwtfrom forum.wtforms import UserForm,LoginUserForm, UpdateUserFormfrom forum import managerfrom forum.models import UserModelfrom forum.handler.BaseHandler import BaseHandlerfrom forum.utils.email_utils import send_mailfrom forum.utils.redis_utils import *from config import secret,emailfrom forum.decorators import login_required_async# 更新用户信息接口class UpdateUserHandler(BaseHandler):async def post(self):rs_data = {}user_form = UpdateUserForm(self.request.arguments)# 验证数据if user_form.validate:id = user_form.id.data# 因为前端传递来的数据前后有空格,所以去掉空格,避免修改登录账号user_form.email.data = user_form.email.data.strip()# peewee_async异步修改数据rs = await manager.execute(UserModel.update(**user_form.data).where(UserModel.id == id))if rs > 0:rs_data['code'] = 200rs_data['msg'] = '更新用户信息成功'else:rs_data['code'] = 500rs_data['msg'] = '更新用户信息失败'else:rs_data['code'] = 500rs_data['msg'] = '数据不合法'self.finish(rs_data)
挂载路由
# router.pyfrom forum.handler import BaseHandler,UserHandlerhandlers = [('/api/user/update/?', UserHandler.UpdateUserHandler)]
2.3效果展示
3用户信息_上传图像并更新到主页展示图片
在修改中,需要上传图片当作头像。
3.1观察前端需要的接口和传递的数据
同样的,还是和更新信息的接口一直,图片pic利用binary来接收即可
需要在前端展示的话,我们需要保证两个点,第一个:我们的user_form需要接收到pic的文件路径(因为更新sql需要使用)第二个:我们的文件路径要保存的完整(检查数据库)
3.2接口代码展示
# 更新用户信息接口class UpdateUserHandler(BaseHandler):async def post(self):rs_data = {}user_form = UpdateUserForm(self.request.arguments)# 验证数据if user_form.validate:id = user_form.id.data# 因为前端传递来的数据前后有空格,所以去掉空格,避免修改登录账号user_form.email.data = user_form.email.data.strip()# 获取头像文件数据pic = self.request.files.get('pic')[0]# 因为有多位用户上传图片,为了避免图片的文件名重复,所以随机生成一个名字来保存file_name = uuid4().hex# 获取源文件的后缀名file_suffix = os.path.splitext(pic.get('filename'))[-1]full_name = file_name + file_suffix# 为了保证用户可以访问到自己上传的头像,因此要把头像上传到静态文件夹中,否则会上传到本文件下full_path = os.path.join(settings.get('static_path'),'img',full_name)# 保存图片头像with open(full_path, 'wb') as f:f.write(pic.get('body'))print(full_path)# 更新用户的头像地址 /static/img/xxx.pnguser_form.pic.data = os.path.join('/static/img',full_name)# peewee_async异步修改数据rs = await manager.execute(UserModel.update(**user_form.data).where(UserModel.id == id))if rs > 0:rs_data['code'] = 200rs_data['msg'] = '更新用户信息成功'else:rs_data['code'] = 500rs_data['msg'] = '更新用户信息失败'else:rs_data['code'] = 500rs_data['msg'] = '数据不合法'self.finish(rs_data)