【Flask微电影】18.电影评论管理:评论列表和删除

个人博客,欢迎查看:https://blog.starmeow.cn/

Github地址:https://github.com/xyliurui/FlaskMovie

评论管理

准备评论数据,添加到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/")
@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评论列表模板

{% include 'admin/alert_info.html' %} {% for comment in page_comments.items %}
User Image
{{ comment.user.name }}   {{ comment.add_time }} 关于电影《{{ comment.movie.title }}》的评论:{{ comment.content }}
删除
{% endfor %}
image.png

评论删除

增加comment_delete删除评论视图

@admin.route("/comment/delete//")
@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评论删除链接

删除
image.png

你可能感兴趣的:(【Flask微电影】18.电影评论管理:评论列表和删除)