Skip to content

Commit

Permalink
encode#4532 Rendering dicts in admin console properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Kahan committed Oct 29, 2016
1 parent 2d3491f commit 88bf243
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 13 deletions.
11 changes: 11 additions & 0 deletions rest_framework/templates/rest_framework/admin/dict_value.html
@@ -0,0 +1,11 @@
{% load rest_framework %}
<table class="table table-striped">
<tbody>
{% for key, value in value.items %}
<tr>
<th>{{ key|format_value }}</th>
<td>{{ value|format_value }}</td>
</tr>
{% endfor %}
</tbody>
</table>
75 changes: 62 additions & 13 deletions tests/test_templatetags.py
Expand Up @@ -41,6 +41,9 @@ def test_format_value_boolean_or_none(self):
self.assertEqual(format_value(None), '<code>null</code>')

def test_format_value_hyperlink(self):
"""
Tests format_value with a URL
"""
url = 'http://url.com'
name = 'name_of_url'
hyperlink = Hyperlink(url, name)
Expand All @@ -54,6 +57,25 @@ def test_format_value_list(self):
self.assertEqual(format_value(list_items), '\n item1, item2, item3\n')
self.assertEqual(format_value([]), '\n\n')

def test_format_value_dict(self):
"""
Tests format_value with a dict
"""
test_dict = {'a': 'b'}
expected_dict_format = """
<table class="table table-striped">
<tbody>
<tr>
<th>a</th>
<td>b</td>
</tr>
</tbody>
</table>"""
self.assertEqual(
format_html(format_value(test_dict)),
format_html(expected_dict_format)
)

def test_format_value_table(self):
"""
Tests format_value with a list of lists/dicts
Expand Down Expand Up @@ -84,20 +106,47 @@ def test_format_value_table(self):
expected_dict_format = """
<tableclass="tabletable-striped">
<tbody>
<tr>
<th>0</th>
<td></td>
</tr>
<tr>
<th>1</th>
<td></td>
</tr>
<tr>
<th>2</th>
<td></td>
</tr>
<tr>
<th>0</th>
<td>
<tableclass="tabletable-striped">
<tbody>
<tr>
<th>item1</th>
<td>value1</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<th>1</th>
<td>
<tableclass="tabletable-striped">
<tbody>
<tr>
<th>item2</th>
<td>value2</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<th>2</th>
<td>
<tableclass="tabletable-striped">
<tbody>
<tr>
<th>item3</th>
<td>value3</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>"""
</table>"""

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

0 comments on commit 88bf243

Please sign in to comment.