多维词典或元组?

时间:2015-07-20 18:52:06

标签: django python-3.x django-templates django-views

我有一个小问题应该很容易解决,但是我被卡住了。 我有以下情况,第一个拉链显示时间轴帖子。 每个时间轴帖子可以有一条或多条评论或根本没有评论。

我首先尝试用jquery来做,但也被卡住了。

因此,为了确保正确的帖子获得正确的评论/我比较post_id和org_post_id的id。 然而,模板内部的循环只出现一次而且效率很低。

我认为多重或元组可以工作,但我根本无法找到如何填充它的答案。 有人请帮忙吗?

谢谢=)

模板:

<div class="col-md-12">
    <section id="cd-timeline" class="cd-container">
     {% for item in merged  %} 
    <div class="cd-timeline-block">
      <div class="cd-timeline-img cd-success">
                <i class="fa fa-tag"></i>
      </div> <!-- cd-timeline-img -->

      <div class="cd-timeline-content">

        <h2>{{item.2}}</h2>
        <p>{{item.6|safe}}</p>
        {% if  item.3 != '<p> </p>'%} 
        <p> <img src="{{item.2|safe}}"/> {% endif %} <p> {{item.4|safe}} {{item.5|safe}} </p> </p>
        <span class="cd-date">Jan 14</span>
            <div class="timeline-item-post">

       <div class="timeline-options">

        <a href="#"><i class="icon-like"></i> {{item.7}}</a>

        <button class="myEvent" id="{{item.9}}"><i class="icon-bubble"></i> {{item.8}} </button>                                                        
        <a href="#"><i class="icon-share"></i>  {{item.9}}</a>

        </div>



                  {% for items in comment_data %}
                    {% if item.0 == items.0 %}

        <div class="timeline-comment">
           <div class="timeline-comment-header">
            <img src="assets/images/avatar2.png" alt="">
            <p> {{items.1}}<small>3 hours ago</small></p>
              </div>
             <p class="timeline-comment-text">{{items.2}}</p>
               </div>


              {% endif %}
            {% endfor %}


        <textarea class="form-control" placeholder="Replay"></textarea>
           </div>  
      </div> <!-- cd-timeline-content -->
    </div> <!-- cd-timeline-block -->
      {% endfor %}

  </section> <!-- cd-timeline -->
  </div>

JSON片段:

  },
  "id": "511524265623184_705647176210891",
  "full_picture": "https://scontent.xx.fbcdn.net/hphotos-xpt1/v/t1.0-9/p720x720/11261437_705647176210891_75015310411838465_n.jpg?oh=f8542855ab46099ea38014411e712ae7&oe=561979D9",
  "from": {
   "id": "511524265623184",
   "category_list": [
    {
     "name": "Bar",
     "id": "110290705711626"
    }
   ],
   "name": "Roberto American Bar",
   "category": "Bar"
  },
  "name": "Roberto American Bar",
  "comments": {
   "paging": {
    "cursors": {
     "before": "Mw==",
     "after": "MQ=="
    }
   },
   "summary": {
    "total_count": 3,
    "order": "ranked"
   },
   "data": [
    {
     "user_likes": false,
     "created_time": "2015-05-20T20:05:31+0000",
     "message": "Mint julep hat gewonnen \ud83c\udf8a\u2728\ud83d\udc90",
     "id": "705647176210891_705801926195416",
     "can_remove": true,
     "like_count": 3,
     "from": {
      "id": "511524265623184",
      "category_list": [
       {
        "name": "Bar",
        "id": "110290705711626"
       }
      ],
      "name": "Roberto American Bar",
      "category": "Bar"
     }
    },
    {
     "user_likes": false,
     "created_time": "2015-05-20T14:24:16+0000",
     "message": "eher ein mint julep; oder? Prost, auf alle f\u00e4lle!",
     "id": "705647176210891_705654729543469",
     "can_remove": true,
     "like_count": 2,
     "from": {
      "name": "Gregor Tom Imhof",
      "id": "991783270834753"
     }
    },
    {
     "user_likes": false,
     "created_time": "2015-05-20T13:46:33+0000",
     "message": "Prince of Wales, klassisch im Silberbecher.",
     "id": "705647176210891_705648629544079",
     "can_remove": true,
     "like_count": 0,
     "from": {
      "name": "Klement Cabana",
      "id": "10200893066687095"
     }
    }
   ]
  }
 }

查看:

for i in feed: 
    try:
        comments.append(i['comments']['summary']['total_count'])
        if i['comments']['data'] != []:

            for n in i['comments']['data']:
                org_post_ids, comment_id  = n['id'].split('_')
                org_post_id.append(org_post_ids)
                comments_name.append(n['from']['name'])
                comments_message.append(n['message'])

                #comments_data.append(i['comments']['data'][0]['message'])



    except KeyError:
        comments.append('0')
show(org_post_id)
comment_data = zip(org_post_id, comments_name, comments_message)

    for i in feed:
        try:
            likes.append(i['likes']['summary']['total_count'])      
        except KeyError:
            likes.append('0')

    for i in feed:
        try:
            picture.append(i['full_picture'])

        except KeyError:
            picture.append('<p> </p>')  


    for i in feed: 
        try:
            little_des.append(i['name'])

        except KeyError:
            little_des.append('<p> </p>')   

    for i in feed: 
        try:
            description.append(i['description'])

        except KeyError:
            description.append('<p> </p>')          
    for i in feed: 
        try:
            message.append(i['message'])

        except KeyError:
            message.append('<p> </p>')      

    for i in feed:
        name.append(i['from']['name'])






    merged = zip(post_id ,profile, name, picture, little_des, description, message, likes, comments, shares)

0 个答案:

没有答案