如何在django模板中显示列表?

时间:2022-01-05 20:22:41

A list is created. I want to display the list in the template.

创建一个列表。我想在模板中显示列表。

search_query=[]
#...
#code here
#...

return render(request, 'query/list.html', {'search_query'})

But it is giving this error - "context must be a dict rather than set."

但它给出了这样一个错误——“上下文必须是一个命令,而不是设置。”

In the list.html

在list.html

{% for c in suggest_search_query%}
    <p>{{c}}</p>
{% endfor %}

1 个解决方案

#1


1  

The error is in the call to render(..):

错误在于调用render(..):

return render(request, 'query/list.html', {'search_query'})
#                                         ^     set      ^

You here did not construct a dictionary, but a set (the notation is a bit similar). A set is a collection of unique hashable values. But you do not map keys to values in a set, that is what a dictionary does.

这里不是构造字典,而是一个集合(符号有点类似)。集合是一组独特的可洗值的集合。但是,您不需要将键映射到集合中的值,这就是dictionary所做的工作。

You need to convert it to:

您需要将其转换为:

return render(request, 'query/list.html', {'suggest_search_query': search_query})

to define a dictionary that maps suggest_search_query to the search_query variable.

要定义一个字典,将suggest_search_query映射到search_query变量。

#1


1  

The error is in the call to render(..):

错误在于调用render(..):

return render(request, 'query/list.html', {'search_query'})
#                                         ^     set      ^

You here did not construct a dictionary, but a set (the notation is a bit similar). A set is a collection of unique hashable values. But you do not map keys to values in a set, that is what a dictionary does.

这里不是构造字典,而是一个集合(符号有点类似)。集合是一组独特的可洗值的集合。但是,您不需要将键映射到集合中的值,这就是dictionary所做的工作。

You need to convert it to:

您需要将其转换为:

return render(request, 'query/list.html', {'suggest_search_query': search_query})

to define a dictionary that maps suggest_search_query to the search_query variable.

要定义一个字典,将suggest_search_query映射到search_query变量。