基于嵌套评论的数据结构


评论的数据结构

comment_list = [
    {'id': 1, 'news_id': 1, 'user_id': 10, 'content': "写的什么玩意呀", 'reply_id': None},
    {'id': 2, 'news_id': 1, 'user_id': 11, 'content': "还真不是玩意 ", 'reply_id': 1},
    {'id': 3, 'news_id': 1, 'user_id': 12, 'content': "写的真好 ", 'reply_id': 1},
    {'id': 4, 'news_id': 1, 'user_id': 11, 'content': "写的真好 ", 'reply_id': 3},
    {'id': 5, 'news_id': 1, 'user_id': 19, 'content': "我勒个去 ", 'reply_id': None},
]


comment_dict = {}
for row in comment_list:
    row['child'] = []
    comment_dict[row['id']] = row

for row in comment_list:
    if row['reply_id']:
        reply_id = row['reply_id']
        comment_dict[reply_id]['child'].append(row)

#结果
for k, v in comment_dict.items():
    print(k, v)
    
# comment_dict = {
#     1:{'id': 1, 'news_id': 1, 'user_id': 10, 'content': "写的什么玩意呀", 'reply_id': None, 'child': [{'id': 2, 'news_id': 1, 'user_id': 11, 'content': "还真不是玩意 ", 'reply_id': 1},{'id': 3, 'news_id': 1, 'user_id': 12, 'content': "写的真好 ", 'reply_id': 1,'child': [{'id': 4, 'news_id': 1, 'user_id': 11, 'content': "写的真好 ", 'reply_id': 3,'child': []},]},]},
#     2:{'id': 2, 'news_id': 1, 'user_id': 11, 'content': "还真不是玩意 ", 'reply_id': 1,'child': []},
#     3:{'id': 3, 'news_id': 1, 'user_id': 12, 'content': "写的真好 ", 'reply_id': 1,'child': [{'id': 4, 'news_id': 1, 'user_id': 11, 'content': "写的真好 ", 'reply_id': 3,'child': []},]},
#     4:{'id': 4, 'news_id': 1, 'user_id': 11, 'content': "写的真好 ", 'reply_id': 3,'child': []},
#     5: {'id': 5, 'news_id': 1, 'user_id': 19, 'content': "我勒个去 ", 'reply_id': None,'child': []},
# }

comment_result = {}
for k, v in comment_dict.items():
    if v['reply_id'] is None:
        comment_result[k] = v

#结果
for k, v in comment_result.items():
    print(k, v)
    
# comment_dict = {
#     1:{'id': 1, 'news_id': 1, 'user_id': 10, 'content': "写的什么玩意呀", 'reply_id': None, 'child': [{'id': 2, 'news_id': 1, 'user_id': 11, 'content': "还真不是玩意 ", 'reply_id': 1},{'id': 3, 'news_id': 1, 'user_id': 12, 'content': "写的真好 ", 'reply_id': 1,'child': [{'id': 4, 'news_id': 1, 'user_id': 11, 'content': "写的真好 ", 'reply_id': 3,'child': []},]},]},
#     5: {'id': 5, 'news_id': 1, 'user_id': 19, 'content': "我勒个去 ", 'reply_id': None,'child': []},
# }
    
View Code

 

递归函数生成评论HTML

def comment(request):
    # models.Comment.objects.filter(news_id=1).values('nid','news_id','user_info_id','content','reply_id')

    comment_list = [
        {'id': 1, 'news_id': 1, 'user_id': 10, 'content': "写的什么玩意呀", 'reply_id': None},
        {'id': 2, 'news_id': 1, 'user_id': 11, 'content': "还真不是玩意 ", 'reply_id': 1},
        {'id': 3, 'news_id': 1, 'user_id': 12, 'content': "写的真好 ", 'reply_id': 1},
        {'id': 4, 'news_id': 1, 'user_id': 11, 'content': "写的真好 ", 'reply_id': 3},
        {'id': 5, 'news_id': 1, 'user_id': 19, 'content': "sdfsfsdsd ", 'reply_id': None},
        {'id': 6, 'news_id': 1, 'user_id': 11, 'content': "你可以趣事了 ", 'reply_id': 2},
        {'id': 7, 'news_id': 1, 'user_id': 11, 'content': "号的", 'reply_id': 6},
    ]

    comment_dict = {}
    for row in comment_list:
        row['child'] = []
        comment_dict[row['id']] = row

    for row in comment_list:
        if row['reply_id']:
            reply_id = row['reply_id']
            comment_dict[reply_id]['child'].append(row)

    commen_reuslt = {}
    for k, v in comment_dict.items():
        if v['reply_id'] == None:
            commen_reuslt[k] = v

    # 根据commen_reuslt生成
    cmt_str = create_html(commen_reuslt)
    return render(request,'comment.html',{'cmt_str': cmt_str})

    
    
def create_child_node(child_comment):
    prev = """
        <div class="comment">
            <div class="content">
        """
    for child in child_comment:
        content = '<div class="item">%s</div>' % child['content']
        prev = prev + content
        if child['child']:
            # 有子评论
            node = create_child_node(child['child'])
            prev = prev + node

    end = """
            </div>
        </div>
        """
    return prev + end

    
    
def create_html(comment_result):
    prev = """
    <div class="comment">
        <div class="content">
    """

    for k, v in comment_result.items():
        content = '<div class="item">%s</div>' % v['content']
        prev = prev + content
        if v['child']:
            # 有子评论
            node = create_child_node(v['child'])
            prev = prev + node

    end = """
        </div>
    </div>
    """
    return prev + end
完整python代码

 

posted @ 2017-06-10 17:44  Vincen_shen  阅读(488)  评论(0)    收藏  举报