1
0
mirror of https://github.com/django/django.git synced 2025-03-09 17:02:43 +00:00

Fixed #36032 -- Rendered URLField values as links in the admin.

This commit is contained in:
antoliny0919 2024-12-25 11:35:27 +09:00 committed by Sarah Boyce
parent 5851605863
commit 97ee8b82c2
6 changed files with 26 additions and 3 deletions

View File

@ -454,6 +454,8 @@ def display_for_field(value, field, empty_value_display, avoid_link=False):
return formats.number_format(value) return formats.number_format(value)
elif isinstance(field, models.FileField) and value and not avoid_link: elif isinstance(field, models.FileField) and value and not avoid_link:
return format_html('<a href="{}">{}</a>', value.url, value) return format_html('<a href="{}">{}</a>', value.url, value)
elif isinstance(field, models.URLField) and value and not avoid_link:
return format_html('<a href="{}">{}</a>', value, value)
elif isinstance(field, models.JSONField) and value: elif isinstance(field, models.JSONField) and value:
try: try:
return json.dumps(value, ensure_ascii=False, cls=field.encoder) return json.dumps(value, ensure_ascii=False, cls=field.encoder)

View File

@ -80,6 +80,8 @@ Minor features
:ref:`extrabody <extrabody>` for adding custom code before the closing :ref:`extrabody <extrabody>` for adding custom code before the closing
``</body>`` tag. ``</body>`` tag.
* The value of a :class:`~django.db.models.URLField` now renders as a link.
:mod:`django.contrib.admindocs` :mod:`django.contrib.admindocs`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -158,8 +158,8 @@ site.register(Parent, NoListDisplayLinksParentAdmin)
class ListDisplayLinksGenreAdmin(admin.ModelAdmin): class ListDisplayLinksGenreAdmin(admin.ModelAdmin):
list_display = ["name", "file"] list_display = ["name", "file", "url"]
list_display_links = ["file"] list_display_links = ["file", "url"]
site.register(Genre, ListDisplayLinksGenreAdmin) site.register(Genre, ListDisplayLinksGenreAdmin)

View File

@ -33,6 +33,7 @@ class GrandChild(models.Model):
class Genre(models.Model): class Genre(models.Model):
name = models.CharField(max_length=20) name = models.CharField(max_length=20)
file = models.FileField(upload_to="documents/", blank=True, null=True) file = models.FileField(upload_to="documents/", blank=True, null=True)
url = models.URLField(blank=True, null=True)
class Band(models.Model): class Band(models.Model):

View File

@ -1059,13 +1059,22 @@ class ChangeListTests(TestCase):
def test_link_field_display_links(self): def test_link_field_display_links(self):
self.client.force_login(self.superuser) self.client.force_login(self.superuser)
g = Genre.objects.create(name="Blues", file="documents/blues_history.txt") g = Genre.objects.create(
name="Blues",
file="documents/blues_history.txt",
url="http://blues_history.com",
)
response = self.client.get(reverse("admin:admin_changelist_genre_changelist")) response = self.client.get(reverse("admin:admin_changelist_genre_changelist"))
self.assertContains( self.assertContains(
response, response,
'<a href="/admin/admin_changelist/genre/%s/change/">' '<a href="/admin/admin_changelist/genre/%s/change/">'
"documents/blues_history.txt</a>" % g.pk, "documents/blues_history.txt</a>" % g.pk,
) )
self.assertContains(
response,
'<a href="/admin/admin_changelist/genre/%s/change/">'
"http://blues_history.com</a>" % g.pk,
)
def test_clear_all_filters_link(self): def test_clear_all_filters_link(self):
self.client.force_login(self.superuser) self.client.force_login(self.superuser)

View File

@ -157,6 +157,7 @@ class UtilsTests(SimpleTestCase):
models.DateField(), models.DateField(),
models.DecimalField(), models.DecimalField(),
models.FloatField(), models.FloatField(),
models.URLField(),
models.JSONField(), models.JSONField(),
models.TimeField(), models.TimeField(),
] ]
@ -196,6 +197,14 @@ class UtilsTests(SimpleTestCase):
display_value, display_value,
) )
def test_url_display_for_field(self):
model_field = models.URLField()
display_value = display_for_field(
"http://example.com", model_field, self.empty_value
)
expected = '<a href="http://example.com">http://example.com</a>'
self.assertHTMLEqual(display_value, expected)
def test_number_formats_display_for_field(self): def test_number_formats_display_for_field(self):
display_value = display_for_field( display_value = display_for_field(
12345.6789, models.FloatField(), self.empty_value 12345.6789, models.FloatField(), self.empty_value