繁体   English   中英

如何添加评论功能 Python Flask MySQL?

[英]How to add a comment feature Python Flask MySQL?

在我们开始之前,我是一个完整的初学者。 这是我目前能做的最好的。 我不知道从哪里开始向我的应用程序添加评论功能。 这就是我现在所拥有的 - 它并不多,但它有效。 这是我的 Python 代码:

@app.route('/forum')
def forum():
    cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
    cursor.execute("SELECT * FROM posts_announcements ORDER BY id DESC LIMIT 1")
    last_post = cursor.fetchone()
    if 'loggedin' in session:
        return render_template('forum.html', last_post=last_post, username=session['username'])
    return render_template('forum.html', last_post=last_post)

@app.route('/forum/announcements/posts/<id>', methods=['GET', 'POST'])
def post(id):
    if 'loggedin' in session:
        cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
        result = cursor.execute("SELECT * FROM posts_announcements WHERE id = %s", [id])
        post = cursor.fetchone()
        return render_template('post.html', post=post, username=session['username'])
    else:
        cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
        result = cursor.execute("SELECT * FROM posts_announcements WHERE id = %s", [id])
        post = cursor.fetchone()
        return render_template('post.html', post=post)

@app.route('/forum/announcements/desc/')
def announcements_desc():
    page = request.args.get(get_page_parameter(), type=int, default=1)
    limit = 20
    offset = page*limit - limit
    if 'loggedin' in session:
        cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
        cursor.execute("SELECT * FROM posts_announcements")
        result = cursor.fetchall()
        total = len(result)
        cur = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
        cur.execute("SELECT * FROM posts_announcements ORDER BY UNIX_TIMESTAMP(date_of_creation) DESC LIMIT %s OFFSET %s;", (limit, offset))
        posts_announcements = cur.fetchall()
        cur.close()
        pagination = Pagination(page=page, per_page=limit, total=total, record_name='posts_announcements')
        return render_template('announcements_desc.html', pagination=pagination, posts_announcements=posts_announcements, username=session['username'])
    else:
        cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
        result = cursor.execute("SELECT * FROM posts_announcements ORDER BY UNIX_TIMESTAMP(date_of_creation) DESC")
        posts_announcements = cursor.fetchall()
        return render_template('announcements_asc.html', posts_announcements=posts_announcements)

@app.route('/forum/announcements/asc')
def announcements_asc():
    if 'loggedin' in session:
        cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
        result = cursor.execute("SELECT * FROM posts_announcements ORDER BY UNIX_TIMESTAMP(date_of_creation) ASC")
        posts_announcements = cursor.fetchall()
        return render_template('announcements_asc.html',posts_announcements=posts_announcements, korisničkoime=session['korisničkoime'])
    else:
        cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
        result = cursor.execute("SELECT * FROM posts_announcements ORDER BY UNIX_TIMESTAMP(date_of_creation) ASC")
        posts_announcements = cursor.fetchall()
        return render_template('announcements_asc.html', posts_announcements=posts_announcements)

所以现在,我已经创建了帖子,我可以从论坛页面访问最后一个帖子。 我还添加了一个选项,可以在点击时按降序和升序对我的帖子进行排序。 但这主要是 MySQL 代码。 这是我的 HTML,这是我的论坛。html

<div class="forums-right-forum">
    <div><a class="reveal_title" href="/forum/announcements/posts/{{ last_post.id }}">{{ last_post.title }}</a></div>
    <span><a href="#">{{ last_post.author }}</a></span>
    <p>{{ last_post.date_of_creation }}</p>
</div>

这是我的帖子。html

<div class="divmid-box-post">
    <div class="date"><p>{{ post.date_of_creation }}</p></div>                  
    <div class="user">
        <a class="post_author" href="#">{{ post.author }}</a>
    </div>
    <div class="post" id="post">
        <div class="message">{{ post.message | safe }}</div>
    </div>
</div>

这是重定向到不同帖子的论坛页面:

<div>
    <ul>
        <a class="post-title" href="/forum/announcements/posts/{{ post.id }}">{{ post.title }}</a>
        <br>
        Author:<a class="author" href="#"> {{ post.author }}</a><p>{{ post.date_of_creation }}</p>
    </ul>
</div>

这一切都按预期工作,但现在我想在我的帖子中添加评论功能,但我不知道如何使用类。 我只有一个 main.py 文件,我所拥有的一切都在里面。 我只是希望有人指出我正确的方向,因为我经历的每篇文章都与 SQLAlchemy 或其他不同的数据库有关。 我相信解决方案可能非常简单,但我就是不明白,正如我之前所说,我是一个完整的初学者。 提前致谢!

编辑:我试过这个,但它没有按预期工作:

@app.route('/forum/announcements/posts/<id>', methods=['GET', 'POST'])
def post(id):
    if 'loggedin' in session:
        cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
        result = cursor.execute("SELECT * FROM posts_announcements WHERE id = %s", [id])
        post = cursor.fetchone()
        cur = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
        result1 = cur.execute("SELECT * FROM comments_announcements")
        comment = cur.fetchall()
        if request.method == 'POST' and 'comment' in request.form:
            comment1 = bbcode.render_html(request.form['comment'])
            author = session['username']
            cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
            cursor.execute("INSERT INTO comments_announcements(message, author) VALUES (%s, %s)", (comment1, author))
            mysql.connection.commit()
            return render_template('post.html', post=post, comment=comment, username=session['username'])
        return render_template('post.html', post=post, comment=comment, username=session['username'])
    return render_template('post.html', post=post, comment=comment)

我将数据输入数据库,但我似乎无法通过 {{ comment.title }} {{ comment.author }} 或 {{ comment.message }} 在我的 html 中显示它

我这样做了,但它不起作用:

{% for comment in comments_announcements %}
            <div class="divmid-box-comment">
                <div class="date_of_creation"><p>{{ comment.date_of_creation }}</p></div>                   
                <div class="author">
                    <a class="post_author" href="#">{{ comment.author }}</a>
                </div>
                <div class="post" id="post">
                    <div class="comment_message">{{ comment.message | safe }}</div>
                </div>
            </div>
{% endfor %}

comment中你有很多评论(也许你应该在最后使用带有 s 的名称)
template内部,您应该使用loop来显示所有评论。

{{ for c in comment }} 
    {{ c.title }} 
    {{ c.author }}
    {{ c.message }}
{{ endfor }}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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