溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

多級(jí)評論的實(shí)現(xiàn)

發(fā)布時(shí)間:2020-08-03 00:29:50 來源:網(wǎng)絡(luò) 閱讀:3053 作者:ck_god 欄目:編程語言

第一種:

comment_list=models.Comment.objects.filter(news_id=new_id)
ret=[]  # 最終拿到的數(shù)據(jù)
comment_list_dict={}  # 構(gòu)建的中間字典
for row in comment_list:  # 通過查到的數(shù)據(jù)中的id作為key,每一行數(shù)據(jù)作為value生成一個(gè)字典
    row.update({"children":[]})  # 構(gòu)建一個(gè)鍵children對應(yīng)一個(gè)空列表
    comment_list_dict[row["id"]]=row  # 將id作為鍵,當(dāng)前行作為值存到該字典中

for item in comment_list:  # 遍歷一遍取到的數(shù)據(jù)列表
    parrent_row=comment_list_dict.get(item["parent_id"])  # 拿到當(dāng)前行對應(yīng)的父親的地址
    if not parrent_row:  # 如果父親是None,則直接進(jìn)入ret中
        ret.append(item)
    else:  # 否則,將這行append到父親的children中
        parrent_row["children"].append(item)  # 重點(diǎn)在這一行,用到了上面提到的第一個(gè)知識(shí)點(diǎn)
print(ret)

第二種:


from django.utils.safestring import mark_safe
# 遞歸查找父節(jié)點(diǎn)
def find_father(dic, comment_obj):
    # 對字典中的每一組元素進(jìn)行循環(huán)操作
    for k, v_dic in dic.items():
        # 如果k等于comment_obj的父節(jié)點(diǎn),那么表示找到了父親。
        if k == comment_obj.parent_comment:
            # 找到了父親,認(rèn)祖歸宗,把自己歸位到父親下面,并給將來的兒子留個(gè)位置
            dic[k][comment_obj] = {}
            # 找到了父親,處理完畢,返回
        else:
            # 剛才沒找到,剝一層,接著往下找。
            find_father(dic[k], comment_obj)

# 遞歸生成html字符串
def generate_comment_html(sub_comment_dic, margin_left_val):
    # 先創(chuàng)建一個(gè)空字符串
    html = ""
    # 對傳入的字典進(jìn)行循環(huán)操作
    for k, v_dic in sub_comment_dic.items():
        html += "<div style='margin-left:%spx'><span class='nickname'>" % margin_left_val + k.name + "</span>" + "<time class='submit-date'>" + str(k.created_time.strftime('%Y-%m-%d %H:%M:%S')) + "<div class='text'>" + k.text + '</div></div><hr>'
        # html += "<div style='margin-left:%spx' class='comment-node'>" % margin_left_val + k.text + "</div>"
        # 有可能v_dic中依然有元素, 遞歸繼續(xù)加
        if v_dic:
            html += generate_comment_html(v_dic, margin_left_val+35)
    # 循環(huán)完成最后返回html
    return html

# 生成層級(jí)評論
@register.simple_tag
def build_comment_tree(comment_list):
    # 定義一個(gè)空字典用來保存轉(zhuǎn)換之后的結(jié)果
    comment_dic = {}
    # 對comment_list中的每個(gè)元素進(jìn)行循環(huán)
    for comment_obj in comment_list:
        # 判斷comment_obj是否存在父節(jié)點(diǎn)。如果沒有,這把該評論作為第一個(gè)節(jié)點(diǎn)
        if comment_obj.parent_comment is None:
            comment_dic[comment_obj] = {}
        else:
            # 否則去找該對象的父節(jié)點(diǎn)。
            find_father(comment_dic, comment_obj)

    # 上面執(zhí)行完畢,comment_dic中會(huì)有轉(zhuǎn)換好的結(jié)果
    # 開始拼接html字符串
    html = "<ul class='comment-list list-unstyled'>"
    # 規(guī)定一個(gè)margin left,每次有遞歸的時(shí)候就往右縮進(jìn)一點(diǎn)。
    margin_left = 0
    # 對comment_dic中的每一組元素進(jìn)行操作
    for k,v in comment_dic.items():
        # 第一層html
        html += "<li class='comment-item'><span class='nickname'>" + k.name + "</span>" + "<time class='submit-date'>" + str(k.created_time.strftime('%Y-%m-%d %H:%M:%S')) + "<div class='text'>" + k.text + '</div></li>'
        # 通過遞歸把他的兒子加上
        html += generate_comment_html(v, margin_left+35)
    # 最后把ul關(guān)上
    html += " </ul>"
    # 關(guān)掉轉(zhuǎn)義
    return mark_safe(html)
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI