Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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.
  • Loading branch information...
commit c94bff2b26f8844fe15d68a79511ff3a6853dc61 1 parent acee46f
Claude Paroz claudep authored
4 django/contrib/admin/helpers.py
View
@@ -7,7 +7,7 @@
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 @@ def contents(self):
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()))
2  django/contrib/admin/templates/admin/edit_inline/tabular.html
View
@@ -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 }}
2  django/contrib/admin/templates/admin/includes/fieldset.html
View
@@ -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 %}
9 tests/admin_views/admin.py
View
@@ -15,6 +15,7 @@
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 @@ def get_prepopulated_fields(self, request, obj=None):
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 @@ def value(self, instance):
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'
1  tests/admin_views/tests.py
View
@@ -3622,6 +3622,7 @@ def test_readonly_get(self):
# 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,
Please sign in to comment.
Something went wrong with that request. Please try again.