Skip to content

Commit 895c67c

Browse files
Alex Kahantomchristie
Alex Kahan
authored andcommitted
1 parent 46f837a commit 895c67c

File tree

2 files changed

+73
-13
lines changed

2 files changed

+73
-13
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{% load rest_framework %}
2+
<table class="table table-striped">
3+
<tbody>
4+
{% for key, value in value.items %}
5+
<tr>
6+
<th>{{ key|format_value }}</th>
7+
<td>{{ value|format_value }}</td>
8+
</tr>
9+
{% endfor %}
10+
</tbody>
11+
</table>

tests/test_templatetags.py

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ def test_format_value_boolean_or_none(self):
4141
self.assertEqual(format_value(None), '<code>null</code>')
4242

4343
def test_format_value_hyperlink(self):
44+
"""
45+
Tests format_value with a URL
46+
"""
4447
url = 'http://url.com'
4548
name = 'name_of_url'
4649
hyperlink = Hyperlink(url, name)
@@ -54,6 +57,25 @@ def test_format_value_list(self):
5457
self.assertEqual(format_value(list_items), '\n item1, item2, item3\n')
5558
self.assertEqual(format_value([]), '\n\n')
5659

60+
def test_format_value_dict(self):
61+
"""
62+
Tests format_value with a dict
63+
"""
64+
test_dict = {'a': 'b'}
65+
expected_dict_format = """
66+
<table class="table table-striped">
67+
<tbody>
68+
<tr>
69+
<th>a</th>
70+
<td>b</td>
71+
</tr>
72+
</tbody>
73+
</table>"""
74+
self.assertEqual(
75+
format_html(format_value(test_dict)),
76+
format_html(expected_dict_format)
77+
)
78+
5779
def test_format_value_table(self):
5880
"""
5981
Tests format_value with a list of lists/dicts
@@ -84,20 +106,47 @@ def test_format_value_table(self):
84106
expected_dict_format = """
85107
<tableclass="tabletable-striped">
86108
<tbody>
87-
<tr>
88-
<th>0</th>
89-
<td></td>
90-
</tr>
91-
<tr>
92-
<th>1</th>
93-
<td></td>
94-
</tr>
95-
<tr>
96-
<th>2</th>
97-
<td></td>
98-
</tr>
109+
<tr>
110+
<th>0</th>
111+
<td>
112+
<tableclass="tabletable-striped">
113+
<tbody>
114+
<tr>
115+
<th>item1</th>
116+
<td>value1</td>
117+
</tr>
118+
</tbody>
119+
</table>
120+
</td>
121+
</tr>
122+
<tr>
123+
<th>1</th>
124+
<td>
125+
<tableclass="tabletable-striped">
126+
<tbody>
127+
<tr>
128+
<th>item2</th>
129+
<td>value2</td>
130+
</tr>
131+
</tbody>
132+
</table>
133+
</td>
134+
</tr>
135+
<tr>
136+
<th>2</th>
137+
<td>
138+
<tableclass="tabletable-striped">
139+
<tbody>
140+
<tr>
141+
<th>item3</th>
142+
<td>value3</td>
143+
</tr>
144+
</tbody>
145+
</table>
146+
</td>
147+
</tr>
99148
</tbody>
100-
</table>"""
149+
</table>"""
101150

102151
list_of_dicts = [{'item1': 'value1'}, {'item2': 'value2'}, {'item3': 'value3'}]
103152
self.assertEqual(

0 commit comments

Comments
 (0)