2011-09-22 23 views
15

Sistema de plantilla Django: cómo obtener el valor del diccionario python de la clave?¿Cómo obtener el valor clave en la plantilla django?

Tengo dos diccionarios que representan datos diferentes pero ambos tienen la misma clave para que pueda acceder a diferentes datos desde la misma clave.

Primera dict es:

{**'Papa, Joey C'**: {'Office Visit Est Pt Level 3 (99213)': 32, 'LAP VENTABD HERNIA 
REPAIR (49652)': 2, 'INSERT TUNNELED CV CATH (36561)': 4, 'Office Visit New Pt 
Level 2 (99202)': 4, 'PUNCTURE/CLEAR LUNG (32420)': 1, 'REPAIR SUPERFICIAL WOUND 
S (12011)': 1, 'DEBRIDE SKINTISSUE (11042)': 29, 'Office Visit New Pt Level 3 (9 
9203)': 11, 'IDENTIFY SENTINEL NODE (38792)': 2, 'MAST MOD RAD (19307)': 1, 'EXC 
FACE LES SC < 2 CM (21011)': 1, 'ACTIVE WOUND CARE20 CM OR (97597)': 4, 'RPR UM 
BIL HERN, REDUC > 5 YR (49585)': 3, 'REMOVE LESION BACK OR FLANK (21930)': 2}} 

Segunda diccionario es:

{**'Papa, Joey C'**: {'10140': 1, '10061': 1, '99214': 1, '99215': 1, '12011': 1, '97606': 1, '49080': 1, '10120': 1, '49440': 1, '49570': 1}, 'Bull, Sherman M': {'99211': 1, '99214': 1, '99215': 1, '99231': 1, '99236': 1, '12051': 1, '15004':1, '47100': 1, '15430': 1, '15431': 1}} 

En plantillas de Django, estoy usando ...

{% for key1,value1 in mydict.items %} 
<br><br> 
<br><br> 
<table border="1"><tr><td>Provider Name</td><td width="70%">{{key1}}</td></tr></table> 
<br><br> 
<table class="report_by_provider"><thead><tr><th>CPT Discription</th><th>Total</th></tr></thead> 
<tbody> 
{% for key2,val2 in value1.items %} 
<tr> 
<td>{{key2}}</td> 
<td>{{val2}}</td> 
</tr> 
{% endfor %} 
</tbody> 
</table> 

<table class="report_by_provider"><thead><tr><th>CPT Code</th><th>CPT Discription</th><th>Vol</th></tr></thead> 
<tbody> 

{% for key3,val3 in mydict1.key1%} 
{% for key,val in val3.items %} 
<tr> 
<td>{{key1}}</td> 
<td>{{val}}</td> 
<td>{{val}}</td> 
</tr> 
{% endfor %} 
{% endfor %} 

Pero el segundo diccionario no está imprimiendo .

+1

Posible duplicado: http://stackoverflow.com/questions/2970244/django-templates-value-of-dictionary-key-with-a-space-in-it – DrTyrsa

Respuesta

38
mydict = {'Papa, Joey C': {'10140': 1, '10061': 1, '99214': 1, '99215': 1, '12011': 1, '97606': 1, '49080': 1, '10120': 1, '49440': 1, '49570': 1}, 'Bull, Sherman M': {'99211': 1, '99214': 1, '99215': 1, '99231': 1, '99236': 1, '12051': 1, '15004':1, '47100': 1, '15430': 1, '15431': 1}} 

{% for mykey,myvalue in mydict.items %} 

    {{ mykey }} : {{ myvalue }} 

{% endfor %} 
+1

No es muy efectivo si solo desea un elemento de una gran frase. – DrTyrsa

+16

Si solo quieres 1 artículo, no enviarías un dict en primer lugar. –

+0

tu ** comentario ** posiblemente haya aclarado por qué mi código era lento ... lol –

8

Dado el diccionario:

{'papa': {'name': 'Papa, Joey C', 'values': {'10140': 1, ... 

se puede acceder a los valores de las claves usando {{ mydict1.papa.name }}

Conociendo directamente usar la llave en la plantilla si contiene espacios de uso o especiales de caracteres, se podría cambiar su estructura (como acabo de hacer para el ejemplo) o crear un custom templatetag/filter que podría usar como {{ mydict1|get_key:"Papa, Joey C"}}.

Si desea un ejemplo completo de filtro, hágamelo saber.

+0

Necesito el ejemplo completo –

Cuestiones relacionadas