I try to compare two dictionaries and if on key, value differs from the other dictionary then print the difference key, value in red.
I think my views.py is correct. But how to show the difference in the template?
So I have views.py:
def data_compare():
fruits = {
"appel": 3962.00,
"waspeen": 3304.07,
"ananas": 24,
}
set1 = set([(k, v) for k, v in fruits.items()])
return set1
def data_compare2():
fruits2 = {
"appel": 3962.00,
"waspeen": 3304.07,
"ananas": 30,
}
set2 = set([(k, v) for k, v in fruits2.items()])
return set2
def data_combined(request):
data1 = data_compare()
data2 = data_compare2()
diff_set = list(data1 - data2) + list(data2 - data1)
print(data1)
return render(request, "main/data_compare.html", context={"data1": data1, "data2": data2, "diff_set": diff_set})
and template:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="container center">
{% for key, value in data1 %}
<span {% if diff_set %} style="color: red;">{% endif %} {{ key }}: {{value}}</span><br>
{% endfor %}
</div>
<div class="container center">
{% for key, value in data2 %}
<span {% if diff_set %} style="color: red;">{% endif %}{{ key }}: {{value}}</span><br>
{% endfor %}
</div>
</body>
</html>
I did a print(diff_set) and that shows:
[('ananas', 24), ('ananas', 30)]
so that is correct
But everything is now red. and only in this case ananas has to be red
Question: how to return the key, value from a dictionary that differes from the other dictionary in red?