質問するログイン新規登録

回答編集履歴

3

追記

2020/01/07 01:36

投稿

quickquip
quickquip

スコア11357

answer CHANGED
@@ -54,4 +54,104 @@
54
54
  {% endfor %}
55
55
  {% endfor %}
56
56
  ```
57
- と短く書けましたが、これも解説したくない、**強く**保守したくない、と思ってしまったので、やはりデータ構造を見直すべきです。
57
+ と短く書けましたが、これも解説したくない、**強く**保守したくない、と思ってしまったので、やはりデータ構造を見直すべきです。
58
+
59
+ ----
60
+ 追記
61
+ ```python
62
+ @register.filter(name='get')
63
+ def get_attr(a, b):
64
+ return a[b]
65
+
66
+ ```
67
+ というカスタムフィルタを定義すると
68
+ ```html
69
+ {% for f in fir %}
70
+ {% with fir_index=forloop.counter0 %}
71
+ <h1>{{ f }}</h1>
72
+ {% for s in sec|get:fir_index %}
73
+ {% with sec_index=forloop.counter0 %}
74
+ <h2>{{ s }}</h2>
75
+ {% for t in third|get:fir_index|get:sec_index %}
76
+ <h3>{{ t }}</h3>
77
+ {% endfor %}
78
+ {% endwith %}
79
+ {% endfor %}
80
+ {% endwith %}
81
+ {% endfor %}
82
+ ```
83
+ となりました。これなら後から理解できそう。
84
+
85
+ ----
86
+
87
+ 質問に書いてあるような、無味乾燥で意味を含まないデータからは判断できない、というのが正直なところです。
88
+
89
+ 単純には
90
+
91
+ ```pyhton
92
+ hoge = {
93
+ 'arr': [
94
+ [
95
+ "1",
96
+ [
97
+ ["1111", ['1111-1', '1111-2']],
98
+ ["1112", ['1112-1', '1112-2']]
99
+ ]
100
+ ],
101
+ [
102
+ "2",
103
+ [
104
+ ["2111", ['2111-1', '2111-2']],
105
+ ["2112", ['2112-1', '2112-2']]
106
+ ]
107
+ ]
108
+ ],
109
+ }
110
+ ```
111
+ となっていれば
112
+ ```html
113
+ {% for f, st in arr %}
114
+ <h1>{{ f }}</h1>
115
+ {% for s, t in st %}
116
+ <h2>{{ s }}</h2>
117
+ {% for t_item in t %}
118
+ <h3>{{ t_item }}</h3>
119
+ {% endfor %}
120
+ {% endfor %}
121
+ {% endfor %}
122
+ ```
123
+ と書けるでしょうし、
124
+
125
+ ```python
126
+ hoge = {
127
+ 'dic': {
128
+ "1":
129
+ {
130
+ "1111": ['1111-1', '1111-2'],
131
+ "1112": ['1112-1', '1112-2'],
132
+ },
133
+ "2":
134
+ {
135
+ "2111": ['2111-1', '2111-2'],
136
+ "2112": ['2112-1', '2112-2'],
137
+ }
138
+ }
139
+ }
140
+ ```
141
+ となっていれば
142
+ ```html
143
+ {% for f, st in dic.items %}
144
+ <h1>{{ f }}</h1>
145
+ {% for s, t in st.items %}
146
+ <h2>{{ s }}</h2>
147
+ {% for t_item in t %}
148
+ <h3>{{ t_item }}</h3>
149
+ {% endfor %}
150
+ {% endfor %}
151
+ {% endfor %}
152
+ ```
153
+ と書けるでしょう。
154
+
155
+ 後者で、辞書のキーになる部分に重複があり得るならこのようには書けないです。また、古いPythonを考慮するなら`OrderedDict`を使うべきです。
156
+
157
+ テンプレート側の書きやすさと見やすさ、Pythonコード側の書きやすさと見やすさのバランスを取っていく必要があるので、表示しようとしているデータの中身がどう作られるのかも明らかでないと判断しにくいですね。

2

追記

2020/01/07 01:36

投稿

quickquip
quickquip

スコア11357

answer CHANGED
@@ -36,4 +36,22 @@
36
36
  結果(spaceless)
37
37
  ```html
38
38
  <h1>1</h1><h2>1111</h2><h3>1111-1</h3><h3>1111-2</h3><h2>1112</h2><h3>1112-1</h3><h3>1112-2</h3><h1>2</h1><h2>2111</h2><h3>2111-1</h3><h3>2111-2</h3><h2>2112</h2><h3>2112-1</h3><h3>2112-2</h3>
39
- ```
39
+ ```
40
+
41
+ ----
42
+
43
+ 追記
44
+ [https://stackoverflow.com/questions/2415865/iterating-through-two-lists-in-django-templates#answer-14857664](https://stackoverflow.com/questions/2415865/iterating-through-two-lists-in-django-templates#answer-14857664)
45
+ にあるzipフィルタを使って
46
+ ```html
47
+ {% for fs, t in fir|zip:sec|zip:third %}
48
+ <h1>{{ fs.0 }}</h1>
49
+ {% for st in fs.1|zip:t %}
50
+ <h2>{{ st.0 }}</h2>
51
+ {% for t in st.1 %}
52
+ <h3>{{ t }}</h3>
53
+ {% endfor %}
54
+ {% endfor %}
55
+ {% endfor %}
56
+ ```
57
+ と短く書けましたが、これも解説したくない、**強く**保守したくない、と思ってしまったので、やはりデータ構造を見直すべきです。

1

些細

2020/01/07 01:01

投稿

quickquip
quickquip

スコア11357

answer CHANGED
@@ -1,4 +1,4 @@
1
- 解説したくない、保守なんて**絶対に**したくない、というようなテンプレートになってしまったのでデータを見直すべきです。
1
+ 解説したくない、保守なんて**絶対に**したくない、というようなテンプレートになってしまったのでデータ構造を見直すべきです。
2
2
  ```html
3
3
  {% for f in fir %}
4
4
  {% with fir_index=forloop.counter %}