Pergunta

I have text file with django template variables like:

Text {{var1}} text - {{var2}}

When I read from file and add to templete:

    {% for item in items %}
    {{ item}}
    {% endfor %}

have a text on the page:

 Text {{var1}} text - {{var2}}

In views.py:

 c={}
 c['var1'] = '10'
 c['var2'] = '20'

How to show variable in template?

Thank you.

Foi útil?

Solução

You can do something like this:

Render your file:

from django.template.loader import render_to_string
rendered = render_to_string('file.txt', {'var1': 'bar', 'var2': 'foo'})

Put this rendered string to your template:

{{ rendered }}
{% for item in items %}
{{ item }}
{% endfor %}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top