我无法从我的 django 项目中的字典中获取值

问题描述 投票:0回答:1
python django dictionary django-models key-value
1个回答
0
投票

您必须从视图传递上下文数据才能在模板中使用它们。

class SampleListView(ListView):

    ...

    def get_context_data(self, **kwargs):
        context = super(SampleListView, self).get_context_data(**kwargs)

        # you should modify following dict
        context["data_you_want_to_use"] =  {
              1 : 'Crew',
              2 : 'Cast',
              3 : 'Director',
              4 : 'Writers',
        }
    return context

    ...

然后,您可以在模板中使用“data_you_want_to_use”,如下所示。

<div>
    {% for key, data in data_you_want_to_use.items %}
        {{ data }}
    {% endfor %}
</div>
© www.soinside.com 2019 - 2024. All rights reserved.