Basic formatting for dict and list values

This commit is contained in:
Tom Christie 2015-05-12 16:18:45 +01:00
parent 9a504efd26
commit a1421cd4a3
5 changed files with 33 additions and 3 deletions

View File

@ -33,6 +33,14 @@ h2, h3 {
margin-right: 1em;
}
td.nested {
padding: 0 !important;
}
td.nested > table {
margin: 0;
}
form select, form input, form textarea {
width: 90%;
}

View File

@ -8,7 +8,7 @@
<tr>
{% for key, value in row.items %}
{% if key in columns %}
<td>
<td {{ value|add_nested_class }} >
{% if key in linked %}<a href="{{ row.url }}">{% endif %}
{{ value|format_value }}
{% if key in linked %}</a>{% endif %}

View File

@ -0,0 +1,11 @@
{% load rest_framework %}
<table class="table table-striped">
<tbody>
{% for item in value %}
<tr>
<th>{{ forloop.counter0 }}</th>
<td>{{ item|format_value }}</td>
</tr>
{% endfor %}
</tbody>
</table>

View File

@ -1,6 +1,7 @@
from __future__ import unicode_literals, absolute_import
from django import template
from django.core.urlresolvers import reverse, NoReverseMatch
from django.template import loader, Context
from django.utils import six
from django.utils.encoding import iri_to_uri, force_text
from django.utils.html import escape
@ -110,9 +111,13 @@ def format_value(value):
if isinstance(value, (int, float, decimal.Decimal, bool, type(None))):
return mark_safe('<code>%s</code>' % value)
elif isinstance(value, list):
return ''
template = loader.get_template('rest_framework/admin/list_value.html')
context = Context({'value': value})
return template.render(context)
elif isinstance(value, dict):
return ''
template = loader.get_template('rest_framework/admin/dict_value.html')
context = Context({'value': value})
return template.render(context)
elif isinstance(value, six.string_types):
if (
(value.startswith('http:') or value.startswith('https:')) and not
@ -124,6 +129,12 @@ def format_value(value):
return six.text_type(value)
@register.filter
def add_nested_class(value):
if isinstance(value, (list, dict)):
return 'class=nested'
return ''
# Bunch of stuff cloned from urlize
TRAILING_PUNCTUATION = ['.', ',', ':', ';', '.)', '"', "']", "'}", "'"]