从2个列表创建字典,在django模板中使用

时间:2017-02-12 20:28:03

标签: python django dictionary

我有2个名单:

year_month_day_list = ['2017-02-12', '2017-02-11', '2017-02-10', '2017-02-09']

filename_prefix_list = ['2017-02-12_11-45-59', '2017-02-12_11-35-05', '2017-02-10_11-00-52', '2017-02-11_10-59-23', '2017-02-09_09-12-09']

如果我可以获取与其关联的ymd匹配的所有文件名:

def filter_files(file_list, filter_param):
    return [f for f in file_list
            if f.startswith(filter_param)]

for unique_day in year_month_day_list:
        files_for_day = sorted(filter_files(
            filename_prefix_list, unique_day), reverse=True)

如何创建一个字典,该字典使用unique_day作为键,并将当天的关联文件名用作值?

输出应该是这样的:

dict = {'2017-02-12': ['2017-02-12_11-45-59', '2017-02-12_11-35-05'], '2017-02-10': ['2017-02-10_11-00-52']}

然后,我如何在模板中使用它们?类似的东西:

<div class="thumbnail-row" id="thumbnail_row">
    <div class="row-date">
        <span>{{ unique_day }}</span>
    </div>
    <div class="">
        <form action="{{ filename_prefix }}/video/" method="post">
            {% csrf_token %}
            <input type="image" name="filename_prefix" value="{{ filename_prefix }}" src="{{ MEDIA_URL}}thumbnails/2017/02/{{ filename_prefix }}.jpg">
        </form>
    </div>
</div>

2 个答案:

答案 0 :(得分:2)

您可以使用itertools.groupby按日期对列表项进行分组。日期可以从 datetime 字符串中切片,并用作分组键。如果 datetime 字符串已按日期聚类,则可以删除sorted

from itertools import groupby
from pprint import pprint

filename_prefix_list = ['2017-02-12_11-45-59', '2017-02-12_11-35-05', '2017-02-10_11-00-52', '2017-02-11_10-59-23', '2017-02-09_09-12-09']

dct = {k: list(g) for k, g in groupby(sorted(filename_prefix_list), lambda x: x[:10])}
pprint(dct)
# {'2017-02-09': ['2017-02-09_09-12-09'],
#  '2017-02-10': ['2017-02-10_11-00-52'],
#  '2017-02-11': ['2017-02-11_10-59-23'],
#  '2017-02-12': ['2017-02-12_11-35-05', '2017-02-12_11-45-59']}

要在模板中使用字典,只需通过视图函数将其传递到当前上下文,并使用items调用其{% for k, v in dct.items %}方法来获取其键值对。

更一般地说:

{% for k, v in dct.items %}
     {% for dt in v %}
     <!- your html -->
     {% endfor %}
{% endfor %}

答案 1 :(得分:0)

要将列表转换为所需格式dict,您可以使用collections.defaultdict作为:

from collections import defaultdict

year_month_day_list = ['2017-02-12', '2017-02-11', '2017-02-10', '2017-02-09']
filename_prefix_list = ['2017-02-12_11-45-59', '2017-02-12_11-35-05', '2017-02-10_11-00-52', '2017-02-11_10-59-23', '2017-02-09_09-12-09']

my_dict = defaultdict(list)
for year_month_day in year_month_day_list:
    for filename_prefix in filename_prefix_list:
        if filename_prefix.startswith(year_month_day):
            my_dict[year_month_day].append(filename_prefix)

my_dict保留的值为:

{
    '2017-02-09': ['2017-02-09_09-12-09'], 
    '2017-02-10': ['2017-02-10_11-00-52'], 
    '2017-02-12': ['2017-02-12_11-45-59', '2017-02-12_11-35-05'],        
    '2017-02-11': ['2017-02-11_10-59-23']
}

要访问Django模板中的dict个对象,您必须执行以下操作:

{% for key, values in my_dict.items %} 
    {{key}}
    {% for value in values %}
        {{value}}
    {% endfor %} 
{% endfor %}

有关详细信息,请参阅:How to access dictionary element in django template?

注意:不要使用dict作为变量名,因为dict是Python中的内置类型。