2013-04-29 12 views
0

저는 장고를 처음 사용하고 있으며 기본적인 질문이 있습니다. 장고 템플릿을 만들었고 외부 변수를 넘겨주고 싶습니다. colspan 태그를 제어합니다. 여러 번 시도했지만 변수를 전달할 수 없습니다. 어떤 도움을 주셔서 감사합니다.장고 템플릿에 변수 전달하기

파이썬 코드 :

def getdjtemplate(th_span="1"): 
    dj_template =""" 
    <table class="out_"> 
    {# headings #} 
     <tr> 
     {% for heading in headings %} 
      <th colspan={{ %s }}>{{ heading }}</th> 
     {% endfor %} 
     </tr> 
    </table> 
    """%(th_span) 
    return dj_template 

나는 내가 이것을 사용하지 않아야 생각하지만, 그것을 해결하는 방법을 잘.

<th colspan={{ %s }}>{{ heading }}</th> 

답변

1

방금 ​​문자열을 반환합니다. django 메서드를 호출하여 템플릿을 렌더링해야합니다.

from django.template import Context, Template 
def getdjtemplate(th_span="1"): 
    dj_template =""" 
    <table class="out_"> 
    {# headings #} 
     <tr> 
     {% for heading in headings %} 
      <th colspan={{ th_span }}>{{ heading }}</th> 
     {% endfor %} 
     </tr> 
    </table> 
    """ 
    t = Template(dj_template) 
    headings = ["Hello"] 
    c = Context({'headings':headings, 'th_span':th_span}) 
    return t.render(c) 
+0

감사합니다. 나는 'th_span': th_span'을 'Context'호출에 포함하는 것을 잊었다. –

관련 문제