mirror of
				https://github.com/django/django.git
				synced 2025-10-26 07:06:08 +00:00 
			
		
		
		
	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:
		| @@ -7,7 +7,7 @@ from django.contrib.admin.templatetags.admin_static import static | |||||||
| from django.core.exceptions import ObjectDoesNotExist | from django.core.exceptions import ObjectDoesNotExist | ||||||
| from django.db.models.fields.related import ManyToManyRel | from django.db.models.fields.related import ManyToManyRel | ||||||
| from django.forms.utils import flatatt | 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.encoding import force_text, smart_text | ||||||
| from django.utils.html import conditional_escape, format_html | from django.utils.html import conditional_escape, format_html | ||||||
| from django.utils.safestring import mark_safe | from django.utils.safestring import mark_safe | ||||||
| @@ -182,6 +182,8 @@ class AdminReadonlyField(object): | |||||||
|                     result_repr = smart_text(value) |                     result_repr = smart_text(value) | ||||||
|                     if getattr(attr, "allow_tags", False): |                     if getattr(attr, "allow_tags", False): | ||||||
|                         result_repr = mark_safe(result_repr) |                         result_repr = mark_safe(result_repr) | ||||||
|  |                     else: | ||||||
|  |                         result_repr = linebreaksbr(result_repr) | ||||||
|             else: |             else: | ||||||
|                 if isinstance(f.rel, ManyToManyRel) and value is not None: |                 if isinstance(f.rel, ManyToManyRel) and value is not None: | ||||||
|                     result_repr = ", ".join(map(six.text_type, value.all())) |                     result_repr = ", ".join(map(six.text_type, value.all())) | ||||||
|   | |||||||
| @@ -47,7 +47,7 @@ | |||||||
|               {% if not field.field.is_hidden %} |               {% if not field.field.is_hidden %} | ||||||
|               <td{% if field.field.name %} class="field-{{ field.field.name }}"{% endif %}> |               <td{% if field.field.name %} class="field-{{ field.field.name }}"{% endif %}> | ||||||
|               {% if field.is_readonly %} |               {% if field.is_readonly %} | ||||||
|                   <p>{{ field.contents|linebreaksbr }}</p> |                   <p>{{ field.contents }}</p> | ||||||
|               {% else %} |               {% else %} | ||||||
|                   {{ field.field.errors.as_ul }} |                   {{ field.field.errors.as_ul }} | ||||||
|                   {{ field.field }} |                   {{ field.field }} | ||||||
|   | |||||||
| @@ -14,7 +14,7 @@ | |||||||
|                     {% else %} |                     {% else %} | ||||||
|                         {{ field.label_tag }} |                         {{ field.label_tag }} | ||||||
|                         {% if field.is_readonly %} |                         {% if field.is_readonly %} | ||||||
|                             <p>{{ field.contents|linebreaksbr }}</p> |                             <p>{{ field.contents }}</p> | ||||||
|                         {% else %} |                         {% else %} | ||||||
|                             {{ field.field }} |                             {{ field.field }} | ||||||
|                         {% endif %} |                         {% endif %} | ||||||
|   | |||||||
| @@ -15,6 +15,7 @@ from django.conf.urls import patterns, url | |||||||
| from django.forms.models import BaseModelFormSet | from django.forms.models import BaseModelFormSet | ||||||
| from django.http import HttpResponse, StreamingHttpResponse | from django.http import HttpResponse, StreamingHttpResponse | ||||||
| from django.contrib.admin import BooleanFieldListFilter | from django.contrib.admin import BooleanFieldListFilter | ||||||
|  | from django.utils.safestring import mark_safe | ||||||
| from django.utils.six import StringIO | from django.utils.six import StringIO | ||||||
|  |  | ||||||
| from .models import (Article, Chapter, Child, Parent, Picture, Widget, | from .models import (Article, Chapter, Child, Parent, Picture, Widget, | ||||||
| @@ -407,8 +408,8 @@ class PrePopulatedPostAdmin(admin.ModelAdmin): | |||||||
| class PostAdmin(admin.ModelAdmin): | class PostAdmin(admin.ModelAdmin): | ||||||
|     list_display = ['title', 'public'] |     list_display = ['title', 'public'] | ||||||
|     readonly_fields = ( |     readonly_fields = ( | ||||||
|         'posted', 'awesomeness_level', 'coolness', 'value', 'multiline', |         'posted', 'awesomeness_level', 'coolness', 'value', | ||||||
|         lambda obj: "foo" |         'multiline', 'multiline_html', lambda obj: "foo" | ||||||
|     ) |     ) | ||||||
|  |  | ||||||
|     inlines = [ |     inlines = [ | ||||||
| @@ -427,6 +428,10 @@ class PostAdmin(admin.ModelAdmin): | |||||||
|     def multiline(self, instance): |     def multiline(self, instance): | ||||||
|         return "Multiline\ntest\nstring" |         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' |     value.short_description = 'Value in $US' | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
| @@ -3622,6 +3622,7 @@ class ReadonlyTest(TestCase): | |||||||
|  |  | ||||||
|         # Checks that multiline text in a readonly field gets <br /> tags |         # Checks that multiline text in a readonly field gets <br /> tags | ||||||
|         self.assertContains(response, "Multiline<br />test<br />string") |         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, "InlineMultiline<br />test<br />string") | ||||||
|  |  | ||||||
|         self.assertContains(response, |         self.assertContains(response, | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user