Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django-显示嵌套在html模板列表中的字典中的值_Python_Django - Fatal编程技术网

Python Django-显示嵌套在html模板列表中的字典中的值

Python Django-显示嵌套在html模板列表中的字典中的值,python,django,Python,Django,我的views.py文件中有以下基于类的视图: class HomeView(TemplateView): template_name = 'annual_means/home.html' site_list = AnnualMean.objects.values("site").distinct() def get_context_data(self, **kwargs): context = super(HomeView, self).get_co

我的views.py文件中有以下基于类的视图:

class HomeView(TemplateView):
    template_name = 'annual_means/home.html'

    site_list = AnnualMean.objects.values("site").distinct()

    def get_context_data(self, **kwargs):
        context = super(HomeView, self).get_context_data(**kwargs)
        context['sites'] = AnnualMean.objects.values("site").distinct()
        return context
在我的html模板的顶部,为了测试这一点,我有{{sites}},它显示: 下面,我有以下html代码:

{% for value in sites %}
    <li>{{value}}</li>
{% endfor %}   
{%用于站点%中的值]
  • {{value}}
  • {%endfor%}
    它列出了如上所示的所有键:值对,例如{'site':'Belfast center'},而不仅仅是值。我假设这是因为QuerySet中的字典嵌套在一个列表中。有什么方法可以让我只返回字典中的内容,或者解决这个问题吗?

    您可以访问:

    字典查找、属性查找和列表索引查找使用点表示法实现:

       {{ my_dict.key }}
       {{ my_object.attribute }}
       {{ my_list.0 }}
    
    对于您的代码,这将是:

    {% for value in sites %}
        <li>{{value.site}}</li>
    {% endfor %}   
    
    {%用于站点%中的值]
    
  • {{value.site}
  • {%endfor%}
    我认为您应该将其用作:

    {% for value in sites %}
        <li>{{value.site}}</li>
    {% endfor %}   
    
    {%用于站点%中的值]
    
  • {{value.site}
  • {%endfor%}

    现在它将打印列表中有效的值,非常感谢!我想我试过了,但可能是site.value