Posting comments

Now that we have our comment form and we understand how to build it, we need to add it to the start of our post view, as follows:

@app.route('/post/<int:post_id>', methods=('GET', 'POST'))def post(post_id):    form = CommentForm()    if form.validate_on_submit():        new_comment = Comment()        new_comment.name = form.name.data        new_comment.text = form.text.data        new_comment.post_id = post_id        try:            db.session.add(new_comment)            db.session.commit()        except Exception as e:            flash('Error adding your comment: %s' % str(e), 'error')            db.session.rollback()        else:            flash('Comment added', 'info')        return redirect(url_for('post', post_id=post_id))    post = Post.query.get_or_404(post_id)    tags = post.tags comments = post.comments.order_by(Comment.date.desc()).all() ...

Get Mastering Flask Web Development - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.