所以我有这个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)


如果有人知道如何解决,请告诉我。
提前致谢!

最佳答案

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

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

关于python - Flat = True的Values_list仍显示括号,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51859200/

10-09 07:31