Fixed #22241 -- Selectively added line breaks in admin read-only fields

When content is supposed to contain HTML, we do not try to add
line breaks in read-only contents.
Thanks Alexander Todorov for the report.
This commit is contained in:
Claude Paroz 2014-03-22 17:00:50 +01:00
parent acee46fc95
commit c94bff2b26
5 changed files with 13 additions and 5 deletions

View File

@ -7,7 +7,7 @@ from django.contrib.admin.templatetags.admin_static import static
from django.core.exceptions import ObjectDoesNotExist
from django.db.models.fields.related import ManyToManyRel
from django.forms.utils import flatatt
from django.template.defaultfilters import capfirst
from django.template.defaultfilters import capfirst, linebreaksbr
from django.utils.encoding import force_text, smart_text
from django.utils.html import conditional_escape, format_html
from django.utils.safestring import mark_safe
@ -182,6 +182,8 @@ class AdminReadonlyField(object):
result_repr = smart_text(value)
if getattr(attr, "allow_tags", False):
result_repr = mark_safe(result_repr)
else:
result_repr = linebreaksbr(result_repr)
else:
if isinstance(f.rel, ManyToManyRel) and value is not None:
result_repr = ", ".join(map(six.text_type, value.all()))

View File

@ -47,7 +47,7 @@
{% if not field.field.is_hidden %}
<td{% if field.field.name %} class="field-{{ field.field.name }}"{% endif %}>
{% if field.is_readonly %}
<p>{{ field.contents|linebreaksbr }}</p>
<p>{{ field.contents }}</p>
{% else %}
{{ field.field.errors.as_ul }}
{{ field.field }}

View File

@ -14,7 +14,7 @@
{% else %}
{{ field.label_tag }}
{% if field.is_readonly %}
<p>{{ field.contents|linebreaksbr }}</p>
<p>{{ field.contents }}</p>
{% else %}
{{ field.field }}
{% endif %}

View File

@ -15,6 +15,7 @@ from django.conf.urls import patterns, url
from django.forms.models import BaseModelFormSet
from django.http import HttpResponse, StreamingHttpResponse
from django.contrib.admin import BooleanFieldListFilter
from django.utils.safestring import mark_safe
from django.utils.six import StringIO
from .models import (Article, Chapter, Child, Parent, Picture, Widget,
@ -407,8 +408,8 @@ class PrePopulatedPostAdmin(admin.ModelAdmin):
class PostAdmin(admin.ModelAdmin):
list_display = ['title', 'public']
readonly_fields = (
'posted', 'awesomeness_level', 'coolness', 'value', 'multiline',
lambda obj: "foo"
'posted', 'awesomeness_level', 'coolness', 'value',
'multiline', 'multiline_html', lambda obj: "foo"
)
inlines = [
@ -427,6 +428,10 @@ class PostAdmin(admin.ModelAdmin):
def multiline(self, instance):
return "Multiline\ntest\nstring"
def multiline_html(self, instance):
return mark_safe("Multiline<br>\nhtml<br>\ncontent")
multiline_html.allow_tags = True
value.short_description = 'Value in $US'

View File

@ -3622,6 +3622,7 @@ class ReadonlyTest(TestCase):
# Checks that multiline text in a readonly field gets <br /> tags
self.assertContains(response, "Multiline<br />test<br />string")
self.assertContains(response, "<p>Multiline<br />html<br />content</p>", html=True)
self.assertContains(response, "InlineMultiline<br />test<br />string")
self.assertContains(response,