美文网首页Flask微电影网站开发
【Flask微电影】19.用户收藏电影:收藏列表和删除

【Flask微电影】19.用户收藏电影:收藏列表和删除

作者: 吾星喵 | 来源:发表于2018-11-11 20:24 被阅读22次

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

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

    收藏管理

    设置从1开始自动递增

    mysql> alter table moviecollect auto_increment=1;
    Query OK, 0 rows affected (0.01 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    
    mysql> insert into
    moviecollect(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.04 sec)
    Records: 6  Duplicates: 0  Warnings: 0
    

    收藏列表

    编辑collect_list收藏列表视图

    @admin.route("/collect/list/<int:page>/")
    @admin_login_require
    def collect_list(page=None):
        if page is None:
            page = 1
        page_moviecollects = MovieCollect.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/collect_list.html', page_moviecollects=page_moviecollects)
    

    编辑collect_list.html收藏列表模板

    <div class="box-body table-responsive no-padding">
        <table class="table table-hover">
            {% include 'admin/alert_info.html' %}
            <tbody>
            <tr>
                <th>编号</th>
                <th>电影</th>
                <th>用户</th>
                <th>添加时间</th>
                <th>操作事项</th>
            </tr>
            {% for moviecollect in page_moviecollects.items %}
                <tr>
                    <td>{{ moviecollect.id }}</td>
                    <td>{{ moviecollect.movie.title }}</td>
                    <td>{{ moviecollect.user.name }}</td>
                    <td>{{ moviecollect.add_time }}</td>
                    <td>
                        <a class="label label-success">编辑</a>
                        &nbsp;
                        <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_moviecollects, 'admin.collect_list') }}
    </div>
    

    收藏删除

    增加collect_delete删除收藏视图

    @admin.route("/collect/delete/<int:delete_id>")
    @admin_login_require
    def collect_delete(delete_id=None):
        moviecollect = MovieCollect.query.get_or_404(delete_id)
        db.session.delete(moviecollect)
        db.session.commit()
        flash('删除收藏成功!', category='ok')
        return redirect(url_for('admin.comment_list', page=1))
    

    修改collect_list.html删除收藏链接

    <a class="label label-danger" href="{{ url_for('admin. collect_delete', delete_id=moviecollect.id) }}">删除</a>
    
    image.png

    相关文章

      网友评论

        本文标题:【Flask微电影】19.用户收藏电影:收藏列表和删除

        本文链接:https://www.haomeiwen.com/subject/xvdffqtx.html