I have a django application. And I have two texboxes where data is displayed coming from two different functions.
So the data is displayed in the texboxes. But the key,value has to be marked red when there is a difference in the two dictionaries. So in this example it is ananas that has the difference.
So I have the TestFile with the data:
class TestFile:
def __init__(self) -> None:
pass
def data_compare2(self):
fruits2 = {
"appel": 3962.00,
"waspeen": 3304.07,
"ananas": 30,
}
set2 = set([(k, v) for k, v in fruits2.items()])
return set2
def data_compare(self):
fruits = {
"appel": 3962.00,
"waspeen": 3304.07,
"ananas": 24,
}
set1 = set([(k, v) for k, v in fruits.items()])
return set1
def compare_dic(self):
set1 = self.data_compare()
set2 = self.data_compare2()
diff_set = list(set1 - set2) + list(set2 - set1)
return diff_set
the views.py:
from .test_file import TestFile
def data_compare(request):
test_file = TestFile()
content_excel = ""
content = ""
content = test_file.data_compare()
content_excel = test_file.data_compare2()
diff_set =test_file.compare_dic()
context = {"content": content, "content_excel": content_excel, "diff_set": diff_set}
return render(request, "main/data_compare.html", context)
and the 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="form-outline">
<div class="form-group">
<textarea class="inline-txtarea form-control" id="content" cols="10" rows="10">
{% for key, value in content %}
<span {% if key in diff_set %} style="color: red;" {% endif %}> {{ key }}: {{value}}</span><br>
{% endfor %}
</textarea>
</div>
</div>
<div class="form-outline">
<div class="form-group">
<textarea class="inline-txtarea form-control" id="content.excel" cols="70" rows="25">
{% for key, value in content_excel %}
<span {% if key in diff_set %} style="color: red;" {% endif %}>{{ key }}: {{value}}</span><br>
{% endfor %}
</textarea>
</div>
</div>
</body>
</html>
The problem I face is that I try to loop over the dictionaries in the template. But the diffrence is not colored red. So this is the output:
<span> waspeen: 3304.07</span><br>
<span> ananas: 24</span><br>
<span> appel: 3962.0</span><br>
so if I do this:
def data_compare(request):
test_file = TestFile()
content_excel = ""
content = ""
content = test_file.data_compare()
content_excel = test_file.data_compare2()
diff_set =test_file.compare_dic()
print(diff_set)
context = {"content": content, "content_excel": content_excel, "diff_set": diff_set}
return render(request, "main/data_compare.html", context)
Then I see in the print statement the correct difference:
[('ananas', 24), ('ananas', 30)]
But how to mark this red in the template?