2014-01-06 3 views
0

django의 템플릿에서 사전을 사용하려고합니다. 그러나 shoudl이 사전의 다른 값에 어떻게 액세스하는지 파악할 수 없습니다.django 템플릿 dictonary 항목을 얻으려면

내보기 :

def index(request): 
    towns = Town.objects.filter(user=request.user) 

    resources = [] 
    for town in towns: 
     resources.append([town, view_resources(town)]) 

    print resources 

    return render(request, 'index.html', {'resources': resources}) 

내 view_resources이 같은 dictonary 반환

{'coin': coin, 'grain': grain, 'iron': iron, 'stone': stone, 'wood': wood,} 

을 내 인쇄이 같은 것을 제공 : 이제

[[<Town: admin's Town>, {'wood': 200, 'stone': 203, 'coin': 176, 'grain': 303, 'iron': 203}]] 

내 템플릿 :

<div> 
    <table> 
    {% for town, resource in resources %} 
     <tr> 
      <td> 
       <h3>{{ town.name }}</h3> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       {{ resources.wood }} 
      </td> 
     </tr> 
    {% endfor %} 
    </table> 
</div> 

"목재"값에 어떻게 액세스 할 수 있습니까?

답변

3

코드에서 resource은 각 튜플의 내부에있는 것이므로 resources이 아닙니다.

{{ resource.wood }} 
+0

고맙습니다. 하하는 지긋 지긋한 오타를 놓 쳤다 :) bene는 그것에 잠시 머물렀다 haha –