个人博客,欢迎查看:https://blog.starmeow.cn/
Github地址:https://github.com/xyliurui/FlaskMovie
会员管理
创建用户头像保存路径
在app/__init__.py
中增加配置文件
app.config['USER_IMAGE'] = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'static/user/') # 存放用户头像的路径
准备一些测试头像放在static/user/
中,然后添加一些数据到user表中
mysql> use movie;
Database changed
mysql> select * from user;
Empty set (0.00 sec)
mysql> insert into
user(name, pwd, email, phone, info, face, uuid)
values
('逗起我', 'pbkdf2:sha256:50000$56hTmVhd$fa3c7d54ead0495e344f', 'user@user01.com', '12800128001', '个人简介', '01.jpg', 'd4d9772d0cb84f6b911a50670f55f395'),
('繁体', 'pbkdf2:sha256:50000$56hTmVhd$fa3c7d54ead0495e344f', 'user@user02.com', '12800128002', '个人简介', '02.jpg', '1fbd3079f1fb44afa7ddd9d212798d18'),
('立体', 'pbkdf2:sha256:50000$56hTmVhd$fa3c7d54ead0495e344f', 'user@user03.com', '12800128003', '个人简介', '03.jpg', '3f7f53f88d894f2cad96e8af4662322d'),
('突然变', 'pbkdf2:sha256:50000$56hTmVhd$fa3c7d54ead0495e344f', 'user@user04.com', '12800128004', '个人简介', '04.jpg', '256974f28e7a43809da96a716957921d'),
('刮胡刀', 'pbkdf2:sha256:50000$56hTmVhd$fa3c7d54ead0495e344f', 'user@user05.com', '12800128005', '个人简介', '05.jpg', '294a495e56474c1ea13962f602aed98d'),
('开机号', 'pbkdf2:sha256:50000$56hTmVhd$fa3c7d54ead0495e344f', 'user@user06.com', '12800128006', '个人简介', '06.jpg', 'd4b67132023b4f3c903c8e3e2b8e84e6'),
('萨达', 'pbkdf2:sha256:50000$56hTmVhd$fa3c7d54ead0495e344f', 'user@user07.com', '12800128007', '个人简介', '07.jpg', '7a3956518f034920a312877899afae0e'),
('认为他', 'pbkdf2:sha256:50000$56hTmVhd$fa3c7d54ead0495e344f', 'user@user08.com', '12800128008', '个人简介', '08.jpg', '1c5e5f7a4e564cffafd827c96e05bc2d'),
('话筒', 'pbkdf2:sha256:50000$56hTmVhd$fa3c7d54ead0495e344f', 'user@user09.com', '12800128009', '个人简介', '09.jpg', '1dd3276fdc8440ff92a537a00c13d07a'),
('机柜号', 'pbkdf2:sha256:50000$56hTmVhd$fa3c7d54ead0495e344f', 'user@user10.com', '12800128010', '个人简介', '10.jpg', 'b4fb51f547b8490188c5a889e26bbb06'),
('珀尔', 'pbkdf2:sha256:50000$56hTmVhd$fa3c7d54ead0495e344f', 'user@user11.com', '12800128011', '个人简介', '11.jpg', '94150271639b4e04855c365691cf3a47'),
('条纹', 'pbkdf2:sha256:50000$56hTmVhd$fa3c7d54ead0495e344f', 'user@user12.com', '12800128012', '个人简介', '12.jpg', '137b329d6e874e93b74082b2c133fad0'),
('与虎添翼', 'pbkdf2:sha256:50000$56hTmVhd$fa3c7d54ead0495e344f', 'user@user13.com', '12800128013', '个人简介', '13.jpg', 'c116ef9e419c447fa06991237f4ece87');
Query OK, 13 rows affected (0.08 sec)
Records: 13 Duplicates: 0 Warnings: 0
mysql> select * from user\G;
*************************** 1. row ***************************
id: 40
name: 逗起我
pwd: pbkdf2:sha256:50000$56hTmVhd$fa3c7d54ead0495e344f
email: user@user01.com
phone: 12800128001
info: 个人简介
face: 01.jpg
add_time: NULL
uuid: d4d9772d0cb84f6b911a50670f55f395
*************************** 2. row ***************************
id: 41
name: 繁体
pwd: pbkdf2:sha256:50000$56hTmVhd$fa3c7d54ead0495e344f
email: user@user02.com
phone: 12800128002
info: 个人简介
face: 02.jpg
add_time: NULL
uuid: 1fbd3079f1fb44afa7ddd9d212798d18
*************************** 3. row ***************************
id: 42
name: 立体
pwd: pbkdf2:sha256:50000$56hTmVhd$fa3c7d54ead0495e344f
email: user@user03.com
phone: 12800128003
info: 个人简介
face: 03.jpg
add_time: NULL
uuid: 3f7f53f88d894f2cad96e8af4662322d
*************************** 4. row ***************************
# 。。。。
呃,忘了添加时间了
mysql> update user set add_time='2018-10-21 12:12:12';
Query OK, 13 rows affected (0.05 sec)
Rows matched: 13 Changed: 13 Warnings: 0
会员列表
修改user_list会员列表视图
@admin.route("/user/list/<int:page>/")
@admin_login_require
def user_list(page=None):
if page is None:
page = 1
page_users = User.query.paginate(page=page, per_page=10)
return render_template('admin/user_list.html', page_users=page_users)
修改user_list.html会员列表模板
<div class="box-body table-responsive no-padding">
{% include 'admin/alert_info.html' %}
<table class="table table-hover">
<tbody>
<tr>
<th>编号</th>
<th>昵称</th>
<th>邮箱</th>
<th>手机</th>
<th>头像</th>
<th>状态</th>
<th>注册时间</th>
<th>操作事项</th>
</tr>
{% for user in page_users.items %}
<tr>
<td>{{ user.id }}</td>
<td>{{ user.name }}</td>
<td>{{ user.email }}</td>
<td>{{ user.phone }}</td>
<td>
<img src="{{ url_for('static', filename='user/'+user.face) }}" width="50px" data-src="holder.js/50x50" class="img-responsive center-block" alt="">
</td>
<td>正常/冻结</td>
<td>{{ user.add_time }}</td>
<td>
<a class="label label-success" href="{{ url_for('admin.user_view') }}">查看</a>
<a class="label label-info">解冻</a>
<a class="label label-warning">冻结</a>
<a class="label label-danger">删除</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="box-footer clearfix">
{% import 'admin/pagination.html' as pg %}
{{ pg.render_pagination(page_users, 'admin.user_list') }}
</div>
image.png
会员详情
修改user_view会员详情视图
@admin.route("/user/view/<int:user_id>/")
@admin_login_require
def user_view(user_id=None):
user = User.query.get_or_404(user_id)
return render_template('admin/user_view.html', user=user)
修改user_view.html会员详情模板
修改user_list.html会员列表显示详情链接
<a class="label label-success" href="{{ url_for('admin.user_view', user_id=user.id) }}">查看</a>
修改user_view.html会员详情
<table class="table table-hover">
<tbody>
<tr>
<td class="td_bd">编号:</td>
<td>{{ user.id }}</td>
</tr>
<tr>
<td class="td_bd">昵称:</td>
<td>{{ user.name }}</td>
</tr>
<tr>
<td class="td_bd">邮箱:</td>
<td>{{ user.email }}</td>
</tr>
<tr>
<td class="td_bd">手机:</td>
<td>{{ user.phone }}</td>
</tr>
<tr>
<td class="td_bd">头像:</td>
<td>
<img src="{{ url_for('static', filename='user/'+user.face) }}" data-src="holder.js/100x100" class="img-responsive" alt="">
</td>
</tr>
<tr>
<td class="td_bd">注册时间:</td>
<td>{{ user.add_time }}</td>
</tr>
<tr>
<td class="td_bd">唯一标志符:</td>
<td>{{ user.uuid }}</td>
</tr>
<tr>
<td class="td_bd">个性简介:</td>
<td>{{ user.info }}</td>
</tr>
</tbody>
</table>
image.png
会员删除
增加user_delete删除会员视图
@admin.route("/user/delete/<int:delete_id>/")
@admin_login_require
def user_delete(delete_id=None):
user = User.query.get_or_404(delete_id)
# 删除同时要从磁盘中删除封面文件
file_save_path = app.config['USER_IMAGE'] # 头像上传保存路径
# 如果存在将进行删除,不判断,如果文件不存在删除会报错
if os.path.exists(os.path.join(file_save_path, user.face)):
os.remove(os.path.join(file_save_path, user.face))
# 删除数据库,提交修改
db.session.delete(user)
db.session.commit()
# 删除后闪现消息
flash('删除会员成功!', category='ok')
return redirect(url_for('admin.user_list', page=1))
修改user_list.html删除会员链接
<a class="label label-danger" href="{{ url_for('admin.user_delete', delete_id=user.id) }}">删除</a>
image.png
网友评论