2011-08-28 3 views
0

의 DB에서 개체 선택 나는이 같은 것을 : 나는 템플릿 매개 변수로 함수 때문에이 라인장고 - tamplate

{% Child.objects.get(mother=mother, father=father) as child %} 

습관을 호출 할 수 없습니다 불행히도

{% for mother in mothers_list %} 
    {% for father in fathers_list %} 
     {% Child.objects.get(mother=mother, father=father) as child %} 
      child.name 

작업. 매번 어떻게 Child 객체를 얻을 수 있을지에 대한 아이디어가 있습니까? 감사합니다.

답변

2

당신은 이것에 대한 custom template tags를 작성할 수 이것은 같은 것입니다 :에서

당신의 project/templatetags/custom_tags.py :

{% load custom_tags %} 

{% for mother in mothers_list %} 
    {% for father in fathers_list %} 
     {{ mother|mother_father:father }} 
0
당신이 할 수있는

:

템플릿에서
from django.template import Library 
    register = Library() 
    @register.filter 
    def mother_father(mother_obj, father_obj): 
      // Do your logic here 
      // return your result 

당신이 좋아하는 템플릿 태그를 사용 view 함수에서의이 처리. 템플릿에서 다음

children_list = [] 
for mother in mothers_list: 
    for father in fathers_list: 
     try: 
      child = Child.objects.get(mother=mother, father=father) 
     except Child.DoesNotExist: 
      child = None 
     children_list.append(child) 

:

{% for c in children_list %} 
    {{ c.name }} 
views.py에서