我在django模板中導致字典的字典時遇到了奇怪的問題。django中字典問題的詞典
data = [{'hrs': 9.0, 'ld': u'pname', 'dn': u'TS', 'ist': u'TS', 'act': u'O', 'date': datetime.date(2011, 9, 19), 'id': 1119556},
{'hrs': 9.0, 'ld': u'pname', 'dn': u'TS', 'ist': u'TS', 'act': u'Ti', 'date': datetime.date(2011, 9, 21), 'id': 1119558},
{'hrs': 2.5, 'ld': u'pname', 'dn': u'TS', 'ist': u'dmin', 'act': u'POC', 'date': datetime.date(2011, 9, 20), 'id': 1119577},
{'hrs': 0.5, 'ld': u'pname', 'dn': u'SMgr', 'ist': u'SMgr', 'act': u'PL', 'date': datetime.date(2011, 9, 20), 'id': 1119578},
{'hrs': 2.0, 'ld': u'pname', 'dn': u'SMgr', 'ist': u'TS', 'act': u'Mting', 'date': datetime.date(2011, 9, 20), 'id': 1119579},
{'hrs': 8.0, 'ld': u'sname', 'dn': u'holiday', 'ist': u'holiday', 'act': u'PL', 'date': datetime.date(2011, 9, 19), 'id': 1119455},
{'hrs': 8.0, 'ld': u'sname', 'dn': u'holiday', 'ist': u'holiday', 'act': u'PL', 'date': datetime.date(2011, 9, 21), 'id': 1119457},
{'hrs': 1.0, 'ld': u'sname', 'dn': u'TS', 'ist': u'TS', 'act': u'O', 'date': datetime.date(2011, 9, 20), 'id': 1119566},
{'hrs': 7.0, 'ld': u'sname', 'dn': u'PD', 'ist': u'PD', 'act': u'LOP', 'date': datetime.date(2011, 9, 20), 'id': 1119567}]
使用字典的這個名單中寫道以下邏輯
results = collections.defaultdict(dict)
for fetch in data:
user = fetch['ld']
get_id = fetch['id']
adata = '%s/%s/%s/%s' % (fetch['dn'], fetch['ist'], fetch['act'], fetch['hrs'])
row = results[user]
row['user'] = user
dt = str(fetch['date'])
row[dt] = adata
這給我下面的輸出
結果:
{u'sname': {'2011-09-21': u'holiday/holiday/PL/8.0', '2011-09-20': u'PD/PD/LOP/7.0', 'user': u'sname', '2011-09-19': u'holiday/holiday/PL/8.0'},
u'pname': {'2011-09-21': u'TS/TS/Ti/9.0', '2011-09-20': u'SMgr/TS/Mting/2.0', 'user': u'pname', '2011-09-19': u'TS/TS/O/9.0'}}
總的來說,我需要得到以下輸出,但字典的字典打印日期關鍵值,而不是多個鍵值對同一日期
預期輸出:
{u'sname': {'2011-09-21': u'holiday/holiday/PL/8.0', '2011-09-20': u'TS/TS/O/1.0', '2011-09-20': u'PD/PD/LOP/7.0', 'user': u'sname', '2011-09-19': u'holiday/holiday/PL/8.0'},
u'pname': {'2011-09-21': u'TS/TS/Trickle/9.0', '2011-09-20': u'TS/dmin/POC/2.5', '2011-09-20': u'SMgr/Smgr/PL/0.5', '2011-09-20': u'SMgr/TS/Mting/2.0' 'user': u'pname', '2011-09-19': u'TechSupport/TechSupport/O/9.0'}}
一個我用下面的下面的模板標籤的哈希表
模板:
@register.filter
def hash(object, attr):
gen_context = { 'object' : object }
try:
value = template.Variable('object.%s' % attr).resolve(gen_context)
except template.VariableDoesNotExist:
value = ' '
return value
HTML:
<table>
<thead>
<th>S.No</th>
<th>Name</th>
{% for dates in week_dates %}
<th>{{dates}}</th>
{% endfor %}
</thead>
<tr>
{% for fetch in data.items %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ fetch.0 }}</td>
{% for dates in week_dates %}
<td>{{ fetch.1|hash:dates }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
任何幫助真的很感激它。
如果您需要更多信息,請讓我知道。
請幫我怎麼做? – sush
你甚至不打擾我的例子嗎? –
得到了重點,但突然想知道如何僞裝一個序列。抱歉讓你煩惱。 – sush