首页 > 解决方案 > 使用烧瓶和 SQLAlchemy 显示 HTML5 评论列表

问题描述

我的烧瓶应用程序需要帮助!我想在我的模板reviews.html中显示帖子的评论列表,但它没有显示评论,评论路径如下:

@app.route('/reviews/<int:post_id>', methods=['GET', 'POST'])
def reviews(post_id = Post.id):
    comment_form = forms.CommentForm(request.form)
    if request.method == 'POST' and comment_form.validate():
        
        user_id = session['user_id']
        comment = Comment(user_id = user_id,
                            post_id = post_id,
                            text = comment_form.comment.data)

        db.session.add(comment)
        db.session.commit()

        success_message = 'Comentario agregado!'
        flash(success_message)

    num = post_id
    posts_tag = db.session.query(Post).filter_by(id=num).first()
    comments_tag = db.session.query(Comment).filter(Comment.post_id==num).all()
    comment_count = Comment.query.count()
    
    return render_template('reviews.html',
                            post = posts_tag, 
                            form = comment_form, 
                            comments = comments_tag,
                            date_format = date_format,
                            comment_count = comment_count)

我的reviews.html 文件如下:

<button type="button" class="collapsible">

  <h5>Comentarios ({{ comment_count }})</h5>

</button>
                    
<ul class="list-group">
  <li class="list-group-item">

    <h5 id="user-comment">{{ comments.username }}</h5> 

    <p id="comment-text">{{ comments.text }}</p>

    <footer>{{ comments.created_date }}</footer>

  </li>
</ul>

我添加我的 model.py 文件以查看它是否与我在数据库中的表有关:

from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from sqlalchemy import MetaData
import datetime

db = SQLAlchemy()
bcrypt = Bcrypt()

class User(db.Model):
    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(50), unique=True)
    email = db.Column(db.String(40))
    password = db.Column(db.String(66))
    comments = db.relationship('Comment')
    posts = db.relationship('Post')
    create_date = db.Column(db.DateTime, default=datetime.datetime.now)

    def __init__(self, username, email, password):
        self.username = username
        self.email = email
        self.password = self.__create_pasword(password)

    def __create_pasword(self, password):
        return bcrypt.generate_password_hash(password).decode ('utf-8')

    def verify_password(self, password):
        return bcrypt.check_password_hash(self.password, password)

class Post(db.Model):
    __tablename__ = 'posts'

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    title = db.Column(db.String(50))
    comment_id = db.relationship('Comment')
    text = db.Column(db.Text())
    created_date = db.Column(db.DateTime, default=datetime.datetime.now)

class Comment(db.Model):
    __tablename__ = 'comments'

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    post_id = db.Column(db.Integer, db.ForeignKey('posts.id'))
    text = db.Column(db.Text())
    created_date = db.Column(db.DateTime, default=datetime.datetime.now)

有什么问题我注意评论,谢谢!

标签: pythonmysqlflask-sqlalchemy

解决方案


解决我的问题如下:

我的main.py文件

num = post_id
posts_tag = db.session.query(Post).filter_by(id=num).first()
comment = db.session.query(Comment).filter(Comment.post_id==num).all()
comment_len = len(comment)

我的review.html文件

<ul class="list-group">
{% for comment in comments %}
  <li class="list-group-item">
    <h5 id="user-comment">{{ comment.text }}</h5>
    <h6>{{ comment.user_id }}</h6>
    <h6>{{ comment.created_date }}</h6>
   </li>
{% endfor %}
</ul>

谢谢,希望对你有帮助!


推荐阅读