zoukankan      html  css  js  c++  java
  • python 多级处理

    多数据提取

    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},
    ]
    
    
    comment_dict = {}
    for row in comment_list:
        row['child'] = []
        comment_dict[row['id']] = row
    
    # a = [1,2,3]
    # b = a
    # b 等于的是a列表的内存地址 , 所有改变b里面的值a里面也会跟着变化
    # 字典也是
    
    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
    print(len(commen_reuslt), commen_reuslt)

    打印结果:

    2 {1: {'child': [
        {'child': [], 'reply_id': 1, 'content': '还真不是玩意 ', 'news_id': 1, 'user_id': 11, 'id': 2},
        {'child': [
            {'child': [], 'reply_id': 3, 'content': '写的真好 ', 'news_id': 1, 'user_id': 11, 'id': 4}
            ],
       'reply_id': 1, 'content': '写的真好 ', 'news_id': 1, 'user_id': 12, 'id': 3}],
       'reply_id': None, 'content': '写的什么玩意呀', 'news_id': 1, 'user_id': 10, 'id': 1}, 
    5: {'child': [], 'reply_id': None, 'content': 'sdfsfsdsd ', 'news_id': 1, 'user_id': 19, 'id': 5}}

    前端页面生成:

    def create_child_html(child_comment):
        prve = """
            <div class="comment">
                <div class="content">
                    <ul class="list-group">
           """
        for child in child_comment:
            tpl = '<li class="list-group-item">%s</li>'
            comment_str = tpl % child['content']
            prve = prve + comment_str
            if child['child']:
                # 有孙子评论,递归执行
                child = create_child_html(child['child'])
                prve = prve + child
        end = """
           </ul>
           </div>
           </div>
           """
        return prve + end
    
    
    def create_html(comment_reuslt):
        prve = """
         <div class="comment">
             <div class="content">
                 <ul class="list-group">
        """
        for k, v in comment_reuslt.items():
            tpl = '<li class="list-group-item">%s</li>'
            comment_str = tpl % v['content']
            prve = prve + comment_str
            if v['child']:
                # 有子评论
                child = create_child_html(v['child'])
                print(v['child'])
                prve = prve + child
        end = """
        </ul>
        </div>
        </div>
        """
        return prve + end

    前端样式(左边距):

    .comment > .content {
           margin-left: 30px;
    }

    多级处理也适用后台的动态菜单

    后台管理,在django中如果有后台,前端、后台要分别建立项目
      - 简单菜单(适用于固定个数菜单)
      - 动态菜单
        用到根菜单和子菜单,就要用到多级处理

  • 相关阅读:
    20200713 T3 图论
    20200713 T1序列问题
    【题解】P1441 砝码称重
    【题解】P2858 [USACO06FEB]Treats for the Cows G/S
    【比赛】AISing Programming Contest 2019
    20200709 T3 城堡
    20200709 T2 括号
    20200709 T1 笔记
    20200628 T3 网络检查
    个人技术总结
  • 原文地址:https://www.cnblogs.com/wangyufu/p/7025966.html
Copyright © 2011-2022 走看看