Reputation: 4434
I am new to Django and have a basic question. I created a Django template, and would like to pass external variables to it, which controls colspan
tag. I tried several times, but could not deliver the variable. I appreciate any help.
Python Code:
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
I think I should not use this, but not sure how to fix it.
<th colspan={{ %s }}>{{ heading }}</th>
Upvotes: 0
Views: 1523
Reputation: 518
You are just returning a string. You must call the django methods to render the template:
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)
Upvotes: 1