本文介绍了如何将Django模板中的{{fieldset.fields}}转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django使用{{fieldset.fields}}返回什么?如何使它成为一个字符串?



在我的模板中,我有这样的:

 code> {%for adminform%} 
< li> {{fieldset.fields}}< / li>
{%ifnanoaddedin fieldset.fields%}
< li> nanoadded在这里< / li>
{%else%}
< li> nanoadded不在这里< / li>
{%endif%}
{%endfor%}

返回:



[('arri','aconcentration','acat','anotes','agtlt','id'),('nanoadded' 'response','select_charc')]
nanoadded不在这里



所以我假设fieldset.fields没有返回一个字符串(即使它看起来像一个字符串)。如何使Django将fieldset.fields的内容看作一个字符串?感谢您的帮助!

解决方案

它看起来像字段属性返回包含两个元组的列表,因此您可能希望通过for循环运行它来检查每个元组的字符串'nanoadded'的成员资格



也许如下所示:

  {%for adminform%} 
{%for fieldset.fields%}
<李> {{field}}< / li>
{%ifnanoaddedin field%}
< li> nanoadded在这里< / li>
{%else%}
< li> nanoadded不在这里< / li>
{%endif%}
{%endfor%}
{%endfor%}


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!

解决方案

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

这篇关于如何将Django模板中的{{fieldset.fields}}转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 05:39