Template tags are not being processed when they are originated from the context.

I have an application that has several partial templates that are loaded as needed by the business rules. In one of these templates, it receives context variables from views.py.
One of these variables, called ‘html_code’, contains HTML code returned from the database with specific form fields that are dynamically defined by the user.
Within this variable, the HTML code has a template tag that needs to call another variable received from the context, ‘random_id’, but it is not being processed and is displayed exactly as it is in the HTML obtained from the database.

Template:

	{% if html %}
		<!-- Custom client information goes here -->
		<div class="row">
			<div class="col-md-12">
				<h4 class="header-title mb-3 pt-2 text-dark d-flex align-items-center">
					Custom info.
				</h4>
			</div>
		</div>

		{{ html_code|safe }}

	{% endif %}

The html code (html_code) obtained from context (views.py):

<div class="row">
     <div class="col-md-12 mb-3">
          <label class="form-label">Shipping Company</label>
          <input type="text" class="form-control" name="shipping_company[{{random_id}}]">
     </div>
</div>

What is being rendered when i test
(look to {{random_id}} not rendered properly)

<div class="row">
    <div class="col-md-12 mb-3">
        <label class="form-label">Transportadora</label>
        <input type="text" class="form-control" name="shipping_company[{{random_id}}]">
    </div>
</div>

What i expect:

<div class="row">
    <div class="col-md-12 mb-3">
        <label class="form-label">Transportadora</label>
        <input type="text" class="form-control" name="shipping_company[{{NDFISOGFDOUF}}]">
    </div>
</div>

How can I make the template tags returned from the context be processed in the template?

You would need to render the html_code template. Rendering a variable in a template does not result in that variable itself being rendered.

What you might want to do is render the template to be added and then add the rendered text to the context.

See render_to_string and Templates | Django documentation | Django.

1 Like

When you include the html_code in your template using the safe filter, the template tags within that string are not processed by the Django template engine. To achieve what you want, you can first replace the template tags in the html_code string with the appropriate context variable values in your view function, and then render it in the template.

Here’s a possible solution:
def your_view(request):
# Your existing code

# Replace the template tag with the value of 'random_id'
html_code = html_code.replace('{{random_id}}', str(random_id))

# Pass the modified 'html_code' to the context
context = {
    'html_code': html_code,
    # other context variables
}

return render(request, 'your_template.html', context)

Then, render the html_code in your template:
{% if html %}




Custom info.



{{ html_code|safe }}

{% endif %}

My solution:

from django.template import Context, Template

In the code, I rendered the variables before rendering the template with the desired context, using the following code line, which shares variables from the final context.

context["html_code"] = Template(html).render(Context({'random_id': random_id}))

Ken’s answer gave me the guidelines. I also tested the method presented by Polevanov and it also worked and is even simpler than the solution I found.
Thank you all again.

2 Likes