Sandra Sandra - 6 months ago 19
Python Question

How turn {{ fieldset.fields }} in Django template into a string?

What does Django return with using {{ fieldset.fields }}? How can I make it a string?

In my template, I have this:

{% for fieldset in adminform %}
<li> {{ fieldset.fields }} </li>
{% if "nanoadded" in fieldset.fields %}
<li> nanoadded is here </li>
{% else %}
<li> nanoadded is NOT here </li>
{% endif %}
{% endfor %}


Here is what is returned:

[('arri', 'aconcentration', 'acat', 'anotes', 'agtlt', 'id'), ('nanoadded', 'response', 'select_charc')]
nanoadded is NOT here

So I am assuming that the fieldset.fields is not returning a string (even though it looks like a string). How can a make Django see the contents of fieldset.fields as a string? Thanks for you assistance!

Answer

It looks like the fields property returns a list that contains two tuples, so you might want to run it through a for loop to check each tuple for the membership of the string 'nanoadded'

Perhaps like this:

{% for fieldset in adminform %}
    {% for field in fieldset.fields %}
    <li> {{ field }} </li>
        {% if "nanoadded" in field  %}
            <li> nanoadded is here </li>
        {% else %}
            <li> nanoadded is NOT here </li>
        {% endif %}       
    {% endfor %} 
{% endfor %}
Comments