Flat = True的Values_list仍显示括号

时间:2018-08-15 12:59:15

标签: python html django python-3.x jinja2

所以我有这个html代码:

<legend><h1 > Weapons that this character has </h1></legend>
    <div>
        <ul style="list-style-type:none" >
            {% if request.GET.idChar %}
            <li >{{ charname }} Has Maihand {{ mh }} and Offhand {{ oh }}</li> 
            {% else %}
            <li>no</li>
            {% endif %} 
        </ul>


    </div>

问题是{{charname}},{{mh}}和{{oh}}仍在我在views.py的一行中添加flat = True后仍显示括号,实际上,它们停止显示括号,但仍显示括号。我还尝试了named = True,named = bool,flat = bool。还尝试使用值而不是values_list ...

Views.py:

def character_weapons (request):
    idchar = request.GET.get('idChar')
    weapons = Characterweapons.objects.filter(characterid__exact=idchar).values_list('weaponid','categoryid_id')
    charname = Characters.objects.filter(characterid__exact = idchar).values_list('name', flat=True)
    print("charname: ")
    print(charname)
    if weapons[0][1] == 1:
        mh_id = weapons[0][0]
        oh_id = weapons[1][0]
    elif weapons[0][1] == 2:
        oh_id = weapons[0][0]
        mh_id = weapons[1][0]
    mh = Weapons.objects.filter(weaponid__exact=mh_id).values_list('weaponname', flat=True)
    oh = Weapons.objects.filter(weaponid__exact=oh_id).values_list('weaponname', flat=True)


    context={"mh": mh, "oh": oh,"idchar": idchar,"charname": charname}

    return render(request,'users/character_weapons.html', context)

如果有人知道如何解决,请告诉我。 预先感谢!

2 个答案:

答案 0 :(得分:2)

values_list()flat=True返回值列表,因此您必须提取索引为0的值,因为查询似乎总是只返回一行:

context={"mh": mh[0], "oh": oh[0],"idchar": idchar,"charname": charname[0]}

答案 1 :(得分:0)

这是一个固定列表,但仍然是一个列表。如果要一次性输出,请使用|join过滤器。

{{ mh|join:"," }}