[Answered ]-Django 1.6 – parse template tags after import/parse from view

2👍

You don’t have to use render, it’s just a shortcut that takes a request, template name and context, and returns an http response with the rendered context.

That’s not what you want in this case. You have a template string rendered that you wish to render. The low level API to render a template is explained here in the docs.

from django.template import Context, Template
from django.http import HttpResponse

def dataview(request):
  ...
  rendered = render_to_string('path/to/template2.html', context)
  template = Template(rendered)
  return HttpResponse(template.render(Context(context)))

You may have to use the verbatim tag so that your blocks and extends tags are not evaluated when you call render_to_string on the first template. For example

{% verbatim %}
{% extends template1.html %}
{% block main_content %}
{% endverbatim %}
{% for DATA in DATALIST %}
{{ DATA|safe }}
{% endfor %}
{% verbatim %}
{% endblock main_content %}
{% endverbatim %}

Leave a comment