简体   繁体   中英

Python Flask | Display post comment

How do i display post comment when i click on the "comment" link?

I just want to show one post from the user, but it only displays the username.

Models.py

class Post(db.Model):
    __searchable__ = ["body"]

    id = db.Column(db.Integer, primary_key=True)
    body = db.Column(db.String(140))
    timestamp = db.Column(db.DateTime)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

def __repr__(self):
    return self.body

Views.py

@app.route("/comment/<username>")
@app.route("/comment/<username>/<int:page>") 
@login_required
def comment(username, page=1):
    form = CommentForm()
    user = User.query.filter_by(username=username).first()  
    if form.validate_on_submit():
        return redirect(url_for('comment'))
    posts = user.followed_posts().paginate(page, POSTS_PER_PAGE, False)
    return render_template("comment.html", form=form, username=username, user=user, posts=posts)

Comment.html

{{ username }}
{{ body }} 
<form action="" method="POST"> 
    {{ form.hidden_tag() }} 
    {{ form.comment(size=auto) }}
    <button class="send-comment"> Kirim </button> 
</form>

I cannot see that you have "returned" the variable "body" to jinja2 when rendering comment.html.

Maybe you should change {{ body }} to {{ posts }} in your "comment.html" file?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM