1👍
There is a lot to say about the code you have shared.
First of all, I am not really sure why you really need a dictionary with this structure. What is your desired output?
Secondly, this code piece is fishy:
for warning in warnings:
if Errors.objects.filter(status='1'):
....
status
is a BooleanField
. You should filter it by a boolean value. On the other hand, why do you need to filter out the whole database in an iteration? Did you mean something like this?
for warning in warnings:
if warning.status:
...
Another thing is the naming you chose. It is a better practice to give your models singular names. Use Error
instead of Errors
.
If you really need to do something like this. I suggest you to use collections.defaultdict
. It will allow adding keys with list
dynamically.
from collections import defaultdict
err = defaultdict(list)
for warning in warnings:
if warning.status:
err['error_numbers'].append(warning.error_number)
err['error_messages'].append(warning.error_message)
err['datetime'].append(warning.datetime)
But again, it seems like you are trying to achieve something hacky. You should probably rethink your needs and approach.
After all this should print out a key and a list for each keys.
<ul>
{% for key, values in err.iteritems %}
<li>{{key}}: #edited here, 1 "}" was missing
{% for value in values %}{{value}}{% endfor %}
</li>
{% endfor %}
</ul>