2016-06-19 2 views
0

여러 개의 중첩 된 dict에서 데이터를 수집하여 템플릿에 전달하고 있습니다. 나는 데이터에 접근하고있어 내 템플릿에서Django 템플릿 필터가 잘못된 데이터를 전달합니다.

@register.filter 
def get_item(dictionary, key): 
    return dictionary.get(key) 

: 내가 키에 의해 딕셔너리에서 데이터를 얻을 수있는 간단한 필터를 쓴 내가 사용하는 경우

{% for location_type_id, location_type_name in location_types.items %} 
    {% with disctrict=disctricts|get_item:location_type_id %} 
     ... 
    {% endwith %} 
{% endfor %} 

을 {{디버그}} I 참조 :

{'districts': {5: {6: 'Friedrichshain'}, 7: {7: 'Kreuzberg', 8: 'Charlottenburg'}}, 
'location_types': {5: 'Bar', 7: '5'}} 

하지만 오류가 발생합니다. 'str'개체에 'get'속성이 없습니다. 필터의 인자를 출력 할 때, 빈 문자열이 dict 대신에 필터에 전달됩니다.

답변

1

아마도 오류가 발생 템플릿에 오타있다 : {% with disctrict=disctricts|get_item:location_type_id %}{% with disctrict=districts|get_item:location_type_id %}

+0

저를 저장해야는! 나는 그 코드를 보면서 몇 시간 동안 앉아 있었다. – Fabian

1

오타가 있습니다. 변수 이름은 districts이지만 템플릿에 disctricts을 사용하고 있습니다.

관련 문제