[Answered ]-Content is null after the first dict()

2👍

request.POST.iteritems() returns a generator object and it is exhausted after the first dict call.

if request.method == "POST":
    q_a = dict(request.POST.iteritems())
    print "first dict(q_a) : %s " % q_a
    print "second dict(q_a): %s " % q_a

0👍

q_a.iteritems() is not a dictionary, it’s a generator (as the print says). You can read more about them on the Python Wiki, but in general you can iterate over them only once. After that, they will raise a StopIteration when you try to iterate over them again (as you do implicitly on you last line using dict(q_a)) which stops the loop. That’s the reason why your call results in an empty dict.

👤poros

0👍

.iteritems() returns a iterator object.

A generator is also an iterator.Generator will provide a sequence of values instead of one as in the case of iterators.

After the first call, it generates the values and then becomes empty. When you do second call, it will show an empty dict.

They are used when you need to use some particular values only once in your code.

Leave a comment