Skip to content Skip to sidebar Skip to footer

Django Use Value Of Template Variable As Part Of Another Variable Name

I currently have this for loop inside my template: {% for i in 1234|make_list %} I would like to obtain something like this inside loop: {{ form.answer_{{ i }} }} I am aware that

Solution 1:

First, you would need a custom template filter to mimic getattr() functionality, see:

Then, you would need add template filter for string concatenation:

{% load getattribute %}

{% for i in 1234|make_list %}    
    {% with "answer_"|add:i as answer %}
        {{ form|getattribute:answer }}
    {% endwith %}
{% endfor %}

Post a Comment for "Django Use Value Of Template Variable As Part Of Another Variable Name"