评论数量不会增加?

来源:11-1 .电影评论-统计

戴晶kyle

2017-12-22

#播放
@home.route('/play/<int:id>/<int:page>/',methods=['GET','POST'])
# @user_login_req
def play(id=None,page=None):
    movie=Movie.query.join(Tag).filter(
        Movie.tag_id==Tag.id,
        Movie.id==int(id)
    ).first_or_404()
    if page is None:
        page=1
    page_data=Comment.query.join(User).filter(
        Comment.movie_id==id,
        Comment.user_id==User.id,
    ).order_by(Comment.addtime.desc()).paginate(page=page,per_page=2)
    movie.playnum += 1
    form=CommentForm()
    if form.validate_on_submit()and 'user' in session:
        data=form.data
        comment=Comment(
            content=data['content'],
            movie_id=int(id),
            user_id=session['user_id'],
        )
        db.session.add(comment)
        db.session.commit()
        movie.commentnum+=1
        flash('添加评论成功!','ok')
        return redirect(url_for('home.play',id=id,page=1))
    db.session.add(movie)
    db.session.commit()
    return render_template('home/play.html',movie=movie,form=form,page_data=page_data)

老师,添加评论后数据库中commentnum并不会增加,一直为0??

写回答

3回答

SuperDi

2018-12-12

老师代码写的有错误

0
0

SuperDi

2018-12-12

去掉return redirect(url_for('home.play',id=id,page=1))

0
0

Grant_Lian

2017-12-23

还需要commit一次吧,

db.session.add(comment)

movie.commentnum+=1

db.session.commit()

这样弄一下

0
0

Python Flask 构建微电影视频网站

Python 最好用轻量级框架,让你轻松胜任 Python Web 工程师

683 学习 · 663 问题

查看课程