簡體   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