簡體   English   中英

Python燒瓶| 顯示帖子評論

[英]Python Flask | Display post comment

當我點擊“評論”鏈接時,如何顯示評論?

我只想顯示來自用戶的一篇文章,但只顯示用戶名。

型號

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>

我看不到您在渲染comment.html時已將變量“ body”“返回”給jinja2。

也許您應該在“ comment.html”文件中將{{ body }}更改為{{ posts }}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM