在帖子下显示评论的聪明方法是什么?

What is the smart way to display comments under posts?

我目前可以将我的帖子和回复写到我的数据库中,但现在我不知道如何显示我对特定帖子的回复以及如何显示带有评论的帖子。任何提示都有助于特别指出有关此类问题的正确文档。

routes.py

@app.route("/post/new", methods=['GET', 'POST'])
@login_required
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(title=form.title.data, content=form.content.data, author=current_user,)
        db.session.add(post)
        db.session.commit()
        flash('New Post Created', 'success')
        return redirect(url_for('home'))
    return render_template('create_post.html', title='New Post', form=form, legend='New Post')


@app.route("/post/<int:post_id>")
def post(post_id):
    post = Post.query.get_or_404(post_id)
    return render_template('post.html', title=post.title, post=post,)


@app.route("/post/reply", methods=['GET', 'POST'])
@login_required
def new_reply():
    form = ReplyForm()
    if form.validate_on_submit():
        reply = Reply(title=form.title.data, content=form.content.data, author=current_user,)
        db.session.add(reply)
        db.session.commit()
        flash('reply posted', 'success')
        return redirect(url_for('home'))
    return render_template('Reply.html', title='New Reply', form=form, legend='New Reply')

post.html

{% block content %}
  <article class="media content-section">
    <img class="article-img" src="{{ url_for('static', filename='profile_pics/' + post.author.image_file) }}">
    <div class="media-body">
      <div class="article-metadata">
        <a class="mr-2" href="{{ url_for('user_posts', username=post.author.username) }}">{{ post.author.username }}</a>
        <small class="text-muted">{{ post.date_posted}}</small>
        {% if post.author == current_user %}
          <div>
            <a class="btn btn-secondary btn-sm mt-1 mb-1" href="{{ url_for('update_post', post_id=post.id) }}">[ Update ]</a>
            <a class="btn btn-secondary btn-sm mt-1 mb-1" href="{{ url_for('new_reply', post_id=post.id) }}">[ Reply ]</a>
            <a class="btn btn-secondary btn-sm mt-1 mb-1" href="{{ url_for('delete_post', post_id=post.id) }}">[ Delete ]</a>
          </div>
        {% else %}
        {% if current_user.is_authenticated %}
        <div>
        <a class="btn btn-secondary btn-sm mt-1 mb-1" href="{{ url_for('new_reply', post_id=post.id ) }}">[ Reply ]</a>
        </div>
        {% endif %}
        {% endif %}
      </div>
      <h2 class="article-title">{{ post.title }}</h2>
      <p class="article-content">{{ post.content }}</p>
    </div>
  </article>
{% endblock content %}
  • 定义一个table叫评论
  • 定义关系 posts-comments 为一对多,具有适当的反向引用
  • 在每个 post 下做:
{%for comment in post.comments%}

{%endfor%}

如果您修改模型以将评论和 post 连接成多对一关系,则可以实现这一点。 (很多评论都与一个post相关)


from app         import db
from flask_login import UserMixin
from datetime    import datetime

class User(UserMixin, db.Model):
    id       = db.Column(db.Integer,     primary_key=True)
    username = db.Column(db.String(64),  unique = True)
    email    = db.Column(db.String(120), unique = True)
    password = db.Column(db.String(500))

class Post(db.Model):
    id          = db.Column(db.Integer, primary_key=True)
    title       = db.Column(db.String(100), nullable=False)
    date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
    content     = db.Column(db.Text, nullable=False)
    user_id     = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)

class Reply(db.Model):
    id          = db.Column(db.Integer, primary_key=True)
    post_id     = db.Column(db.Integer, db.ForeignKey('post.id'), nullable=False)
    user_id     = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    content     = db.Column(db.Text, nullable=False)
    date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
    

现在您可以通过使用 post_id

简单地查询回复 table 来获取 post 的所有回复
from .models import Reply

replies = Reply.query.filter_by(post_id=`your_post_id_here').all()

您可以获取 replies 列表并将其呈现在模板中