2017-01-04 1 views
1

私はdictの持っている:私は今のところdjangoテンプレート - テーブルの重複を隠すには?

+--------+------------------------------+ 
| 1 |        | 
+--------+------------------------------+ 
| 2 |        | 
+--------+------------------------------+ 
| 3 | some_text     | 
+--------+        + 
| 4 |        | 
+--------+        + 
| 5 |        | 
+--------+------------------------------+ 
| 6 |        | 
+--------+------------------------------+ 
| 7 | other_text     | 
+--------+------------------------------+ 
| 8 |        | 
+--------+------------------------------+ 

{% for skey, svalue in sdict.items %} 
    <tr> 
     <td> 
      {{ skey }} 
     </td> 
     <td> 
      {% for val in svalue %} 
       {{ val }} 
      {% endfor %} 
     </td> 
    </tr> 
{% endfor %} 

と出力:

+--------+------------------------------+ 
| 1 |        | 
+--------+------------------------------+ 
| 2 |        | 
+--------+------------------------------+ 
| 3 | some_text     | 
+--------+------------------------------+ 
| 4 | some_text     | 
+--------+------------------------------+ 
| 5 | some_text     | 
+--------+------------------------------+ 
| 6 |        | 
+--------+------------------------------+ 
| 7 | other_text     | 
+--------+------------------------------+ 
| 8 |        | 
+--------+------------------------------+ 

my_dict = { 
    '1': [], 
    '2': [], 
    '3': ['some_text'], 
    '4': ['some_text'], 
    '5': ['some_text'], 
    '6': [], 
    '7': ['other_text'], 
    '8': [] 
} 

そして、私はテンプレートでこれを表示するに

方法それ?

+0

"['some_text']"リストは常に単一の値になるか、複数の値が含まれる可能性がありますか? –

+0

シングルですがリストに含まれていなければなりませんが、リストを削除しようとしています – Nips

+0

djangoビュー自体からさらに情報を送ることができますか? –

答えて

0

{% ifchanged %}タグとともに、{% regroup %}タグを辞書の変換バージョンとともに使用できます。これは、そのようにように、あなたのテンプレートで使用することができます

sdict_converted = [{"column_one": k, "column_two": v} for k, v in sdict.items()] 

まず、あなたは、例えば、{% regroup %}タグと連動何かにあなたのビューに辞書のリストを自分の辞書を変換する必要があるだろう:

{% regroup sdict_converted|dictsort:"column_one" by "column_two" as grouped_sdict_converted %} 

{% for group in grouped_sdict_converted %} 
    {% for item in group.list %} 
    <tr> 
     <td> 
     {{ item.column_one }} 
     </td> 
     {% ifchanged item.column_two %} 
     <td rowspan="{{ group.list|length }}"> 
      {% for nested_value in item.column_two %} 
       {{ value }} 
      {% endfor %} 
     </td>      
     {% endifchanged %} 
    </tr> 
    {% endfor %} 
{% endfor %} 
関連する問題