【问题标题】:How to access a dictionary in a list in Django Template?如何访问 Django 模板列表中的字典?
【发布时间】:2015-08-18 04:26:32
【问题描述】:

在我的views.py中我有:

credits_total = Course.objects.filter(dept = dept_code1, b_tech = 1, type = 1).values('sem').annotate(sum = Sum('credits'))

所以 credits_total 有以下内容:

credits_total = [{'sem': 3, 'sum': 19},{'sem': 4, 'sum': 20},{'sem': 5, 'sum': 21},{'sem':6,'sum':22},{'sem':7,'sum':21},{'sem':8,'sum':24}]

我想做这样的事情:

{% for i in "012345" %}
    ....
    <b> {{credits_total[i]['sum']}} </b>
    ...
{% endfor %}

如何在列表中的特定索引处访问字典中的特定键?

我是 django 新手,请您详细解释一下。谢谢!

【问题讨论】:

标签: django templates


【解决方案1】:

这成功了: 在views.py中:

    from itertools import izip_longest
    def course(request, dept_code):
    ....
    sum_credits_total = list()
    ....
    for i in credits_total: sum_credits_total.append(i['sum'])
    ....
    total_list  = izip_longest(sum_credits_total,hours_total,sum_sem_credits,sum_sem_hours)
    context_dict['total_list'] = total_list
    return render (request,'course.html', context_dict)

在模板中:

    {% for i,j,k,l in total_list %}
         {{i}}....{{j}}....{{k}}....{{l}}
    {% endfor %}

【讨论】:

    【解决方案2】:

    Python 字典最好被认为是unordered sets。您不应该尝试对它们进行索引...也就是说,您可以iterate over your dictionary data 并按如下方式打印。

    {% if credits_total %}
        {% for item in credits_total %}
            sem:{{ item.sem }}, sum:{{ item.sum }}
        {% endfor %}
    {% endif %}
    

    【讨论】:

    • 即使这似乎也没有给我结果。它没有任何价值!
    • 您在 credits_total 中看到了什么吗?也许您没有正确传递数据。使用您的确切数据,这对我来说很好。
    • 确切地说,我在views.py中做这样的事情:context_dict['credits_total'] = credits_totalbtech_list = zip(collapse_num,sem_title_btech,theory_btech,practical_btech,integers) 其中integers = [0,1,2,3,4,5]和`context_dict['btech_list'] = btech_list and 返回渲染( request,'course.html', context_dict) ` 然后在 django 模板中 {% for i,j,k,l,m in btech_list %} .....` {{credits_total.m.sum}}. But this doesn't give any data at all. It gives the results when i do {{credits_total.1.sum}}` 或直接输入任何其他数字。
    • 你为什么试图通过迭代btech_list来转储credits_total字典?简化...从等式中删除btech_listcredits_total 完全是一个不同的对象,应该单独处理。
    【解决方案3】:

    在模板中,您可以使用点符号。 像这样:

    <b>{{credits_total.i.sum}}</b>
    

    但问题在于循环内的i var 不是数字。 您需要编写自己的标签过滤器才能在 python 中获得类似于常规range() 的内容。 看到这个:create range filter

    【讨论】:

    • 我试过了,但是数据不显示也不抛出任何错误!!
    • 我尝试通过将 i 替换为 0 来对其进行硬编码。它仍然不起作用!
    • 我尝试使用数字而不是 i,它对我有用。 {{credits_total.1.sum}}
    • 确切地说,我在views.py中做这样的事情:context_dict['credits_total'] = credits_totalbtech_list = zip(collapse_num,sem_title_btech,theory_btech,practical_btech,integers) 其中integers = [0,1,2,3,4,5]和`context_dict['btech_list'] = btech_list and 返回渲染( request,'course.html', context_dict) ` 然后在 django 模板中 {% for i,j,k,l,m in btech_list %} .....` {{credits_total.m.sum}}. But this doesn't give any data at all. It gives the results when i do {{credits_total.1.sum}}`。现在 m 是一组 int 值,但它仍然没有给出数据!
    猜你喜欢
    • 2021-10-17
    • 2016-02-29
    • 1970-01-01
    • 2018-09-11
    • 1970-01-01
    • 2021-06-16
    • 2011-07-11
    • 2021-10-17
    相关资源
    最近更新 更多