【百度云搜索,搜各种资料:http://www.81ad.cn】
Flask 构建微电影视频网站
已上线演示地址: http://movie.tbquan.cn
评论管理
准备评论数据,添加到comment表中
mysql> use movie;
Database changed
mysql> select * from comment;
Empty set (0.00 sec)
mysql> insert into
comment(content, movie_id, user_id, add_time)
values
('好看', 5, 51, '2018-10-21 16:16:16'),
('还可以', 8, 45, '2018-10-21 16:16:16'),
('很精彩', 5, 50, '2018-10-21 16:16:16'),
('场面真精彩', 8, 48, '2018-10-21 16:16:16'),
('值得一看', 5, 43, '2018-10-21 16:16:16'),
('还不错', 8, 50, '2018-10-21 16:16:16');
Query OK, 6 rows affected (0.11 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> select * from comment;
+----+-----------------+----------+---------+---------------------+
| id | content | movie_id | user_id | add_time |
+----+-----------------+----------+---------+---------------------+
| 1 | 好看 | 5 | 51 | 2018-10-21 16:16:16 |
| 2 | 还可以 | 8 | 45 | 2018-10-21 16:16:16 |
| 3 | 很精彩 | 5 | 50 | 2018-10-21 16:16:16 |
| 4 | 场面真精彩 | 8 | 48 | 2018-10-21 16:16:16 |
| 5 | 值得一看 | 5 | 43 | 2018-10-21 16:16:16 |
| 6 | 还不错 | 8 | 50 | 2018-10-21 16:16:16 |
+----+-----------------+----------+---------+---------------------+
6 rows in set (0.00 sec)
评论列表
修改comment_list评论列表视图
@admin.route("/comment/list/<int:page>")
@admin_login_require
def comment_list(page=None):
if page is None:
page = 1
page_comments = Comment.query.join(
Movie
).join(
User
).filter(
Movie.id == Comment.movie_id,
User.id == Comment.user_id
).order_by(
Comment.add_time.desc()
).paginate(page=page, per_page=10)
return render_template('admin/comment_list.html', page_comments=page_comments)
修改comment_list.html评论列表模板
<div class="box-body box-comments">
{% include 'admin/alert_info.html' %}
{% for comment in page_comments.items %}
<div class="box-comment">
<img class="img-circle img-sm"
src="{{ url_for('static',filename='user/'+comment.user.face) }}" alt="User Image">
<div class="comment-text">
<span class="username">
{{ comment.user.name }}
<span class="text-muted pull-right">
<i class="fa fa-calendar" aria-hidden="true"></i>
{{ comment.add_time }}
</span>
</span>
关于电影<a>《{{ comment.movie.title }}》</a>的评论:{{ comment.content }}
<br><a class="label label-danger pull-right">删除</a>
</div>
</div>
{% endfor %}
</div>
<div class="box-footer clearfix">
{% import 'admin/pagination.html' as pg %}
{{ pg.render_pagination(page_comments, 'admin.comment_list') }}
</div>
BLOG_20181111_202158_76
评论删除
增加comment_delete删除评论视图
@admin.route("/comment/delete/<int:delete_id>/")
@admin_login_require
def comment_delete(delete_id=None):
comment = Comment.query.get_or_404(delete_id)
db.session.delete(comment)
db.session.commit()
flash('删除评论成功!', category='ok')
return redirect(url_for('admin.comment_list', page=1))
修改comment_list.html评论删除链接
<a class="label label-danger pull-right" href="{{ url_for('admin.comment_delete', delete_id=comment.id) }}">删除</a>
BLOG_20181111_202221_53
网友评论