QuerySet : A Django class that processes SQL responses and returns a python construct representing the results. Although it functions as a list in different ways, it is actually what is called iterable. It just pokes fun at the behavior of the list so you can use things like for-loops on it.
Tuple : fixed list. This means that after installing it, it cannot be changed. In almost any other way, it behaves exactly like a list.
Dictionary . Also known as a hash in other languages. It can be considered a “key list”. A “list” in the strictest sense is a group of objects stored sequentially in memory. In the old days of programming, you would have to “pull out” items and “click” items on a list, and you could only get them in FIFO or in the first order. Dictionaries provide a way to search for items in a list. It consists of key-value pairs, so you can refer to the key and get the attached value.
Now in terms of Django templates:
QuerySets : you iterate over them using standard methods. Having received the result from MyModel.objects.all() , you can use the {% for value in queryset %} tag.
Tuples . As for iterations, they behave like standard lists. You can also just use the {% for value in tuple %} tag. The only potential hang is that sometimes you get tuples of tuples or a list of tuples. They look like multi-level lists. You have to iterate over the external list or tuple, and then iterate over the internal ones.
Dictionaries : they are probably the most complex, just because they need a method call to get an iterative object.
mydict.iteritems()
The last method is probably the best of a simple iteration in a Django template:
{% for value in mydict.itervalues %}
source share