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

*爱你&永不变心* 提交于 2019-12-10 11:46:06

问题


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!


回答1:


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 %}


来源:https://stackoverflow.com/questions/37364585/how-turn-fieldset-fields-in-django-template-into-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!