简体   繁体   English

烧瓶线程注释

[英]Flask threaded comments

I would like to display nested comments in Flask. 我想在Flask中显示嵌套评论。 I use MongoDb and my document structure is like this: 我使用MongoDb,我的文档结构如下:

{"_id":16,"content":"This is first answer.","discussion_id":1,
"posted":{"$date":"2017-10-26T19:19:05.174Z"}}
{"_id":17,"content":"This is second answer.","discussion_id":1,
"posted":{"$date":"2017-10-26T19:19:27.325Z"}}
{"_id":18,"content":"This is third answer.","discussion_id":1,
"posted":{"$date":"2017-10-26T19:20:00.126Z"}}
{"_id":19,"content":"This is fourth answer.  This answer's parent should be second.","discussion_id":1,
"posted":{"$date":"2017-10-26T19:21:28.206Z"},"parentid":2}
{"_id":20,"content":"Fifth answer whose parent should be fourth.","discussion_id":1,
"posted":{"$date":"2017-10-26T19:22:11.393Z"},"parentid":4}

Test python program looks like this: 测试python程序如下所示:

from flask import render_template
from flask import Flask
from flask_pymongo import PyMongo

app = Flask(__name__)
app.config['MONGO_DBNAME'] = 'programming'
app.config['MONGO_URI'] = 'mongodb://localhost:27017/programming'
app.config['JSON_AS_ASCII'] = False
mongo = PyMongo(app)

@app.route('/')
def index():
    table = mongo.db.comments

    commentList = table.find({'discussion_id' : 1})

    comments = []
    for comment in commentList:
        comments.append({'commentnumber' : comment['_id'], 'date' : comment['posted'], 'content' : comment['content']})
        result = mongo.db.comments.find_one( { '_id' : comment['_id'] , "parentid": { '$exists': True, '$ne': False } })
        if (result):
            comments.append({ 'parent' : comment['parentid'] })
            print("Parent comment ", comment['parentid'])

    return render_template('index.html', comments=comments)

if __name__ == "__main__":
    app.run(debug=True)

And jinja template I would like to display comments recursively. 和jinja模板,我想递归显示评论。

{%- for item in comments recursive %}
<li>{{ item.content }}</li>
{%- if item.children -%}
<ul class="children">{{ loop(item.children) }}</ul>
{%- endif %}</li>
{%- endfor %}

How should one store current post's children and display nested comments recursively in Jinja. 一个人应该如何存储当前帖子的子级,并在Jinja中递归地显示嵌套的评论。

I did something similar on the forums for the Flask site for my game Infinitroid, eg https://infinitroid.com/forum/posts/12 我在我的游戏Infinitroid的Flask网站论坛上做了类似的事情,例如https://infinitroid.com/forum/posts/12

What I basically did is, on the server side, use the parent-id of each comment to determine an integer depth or indent level: if you sort by primary key, then later comments come after eariler ones, and the parent should already be on the list. 我基本上所做的是,在服务器端,使用每个注释的父ID来确定整数depth或缩进级别:如果按主键排序,则后面的注释会放在较早的注释之后,并且父注释应该已经在名单。 So you can set depth = 0 for comments with no parent, and parent.depth + 1 for those with a parent (use a temporary dictionary for lookup). 因此,您可以为没有父级的注释设置depth = 0为有父级的注释设置parent.depth + 1 (使用临时词典进行查找)。

Set up your CSS to indent based on tag nesting level. 将您的CSS设置为根据标签嵌套级别缩进。 And then, use the algorithm below for display. 然后,使用下面的算法进行显示。 In my case I display the comments via javascript (you can view source on that page if you're curious) but the algorithm should be doable in Jinja too. 就我而言,我通过javascript显示注释(如果您好奇,可以在该页面上查看源代码),但是该算法也应在Jinja中可行。

Start at depth=0. 从深度= 0开始。 For each comment: 对于每个评论:

  • if dropping a depth level, add closing tag for each level dropped 如果下降深度级别,则为每个下降的级别添加结束标记
  • add an opening tag for this comment and increment depth level 为此评论添加一个开始标签并增加深度级别
  • display the comment body 显示评论正文
  • on the last comment, add closing tags for any remaining depth levels 在最后一条评论上,为所有剩余深度级别添加结束标记

You can structure this as a "loop with exit" loop to avoid duplicating the closing-depth-levels part. 您可以将其构造为“带有退出的循环”循环,以避免重复闭合深度级别部分。

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

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