简体   繁体   English

django模板中如何遍历此特定数据(列表中的dict是另一个列表中的元组的第二个值)?

[英]How could iterate over this specific data (dict inside a list which is the second value of a tuple inside another list) in a django template?

How could I take the below collection and iterate over it in a django template ? 我如何才能将以下集合带入Django模板中并对其进行迭代?

[('Foo Key', [{'lname': 'Bar', 'fname': 'Foo'}])]

Please note the above example is much smaller just to keep things simple. 请注意,上面的示例要小得多,只是为了保持简单。 If we need the fully expanded collection, eg if size matters, I can update this post. 如果我们需要完全扩展的集合,例如,如果大小很重要,我可以更新此帖子。

My closest successful attempt is noted below. 我最成功的尝试如下。

{% for key0, value0 in data %}
    <tr>
        <td> key0 {{ key0 }}:</td>
        <td> value0 {{ value0 }} </td>
    </tr>

    <p> {% for value1 in value0 %}
        <td> {{ value1 }}</td>
    {% endfor %}
    </p>

{% endfor %}

and this will leave me with the below output. 这将使我获得以下输出。

Foo Key {'lname': 'Bar', 'fname': 'Foo'} Foo键{'lname':'Bar','fname':'Foo'}

I cannot seem to get 'Bar' or 'Foo' out of it. 我似乎无法从中脱颖而出。

Some other context Here is the view where the data is produced 其他一些上下文这是生成数据的视图

from django.shortcuts import render
from .models import Person, PersonMeta    

# Create your views here.
def thanks(request):
    return render(request, 'thanks.html')    


from .forms import NameForm


def home(request):
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = NameForm(request.POST)
        # check whether it's valid:
        if form.is_valid():
            data = {}
            # process the data in form.cleaned_data as required
            currentUser = request.user
            person = Person.objects.create(first_name=currentUser.first_name, last_name=currentUser.last_name,
                                           nickname=form.cleaned_data['nickname'])
            # getting front loaded personMeta
            personDetails = PersonMeta.objects.filter(frontLoaded_first_name=currentUser.first_name,frontLoaded_last_name=currentUser.last_name).values()

            # setting key
            currUserKey = "{0} {1}".format(currentUser.first_name, currentUser.last_name)

            # if person details is not false
            # this if may not have to be here. 
            if (personDetails):
                data[currUserKey] = personDetails    

                # redirect to a new URL:
        return render(request, 'signUp.html', {'data': sorted(data.items())})    

    # if a GET (or any other method) we'll create a blank form
    #
    else:
        form = NameForm()    

        return render(request, 'home.html', {'form': form})

You have a dict inside a list which is the second value of a tuple inside another list : 您在list有一个dict ,这是另一个list tuple的第二个值:

[('Foo Key', [{'lname': 'Bar', 'fname': 'Foo'}])]

So here we go: 所以我们开始:

{% for tuple in data %}
    <p>Tuple key: {{ tuple.0 }}</p>
    {% for key, value in tuple.1.0.items %}
        <p>Dict key, value: {{ key }}: {{ value }}</p>
    {% endfor %}
{% endfor %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM