[Django] #35544: Readonly Widget Template

10 views
Skip to first unread message

Django

unread,
Jun 20, 2024, 11:42:19 AM6/20/24
to django-...@googlegroups.com
#35544: Readonly Widget Template
-------------------------------------+-------------------------------------
Reporter: Sven R. | Owner: nobody
Kunze |
Type: New | Status: new
feature |
Component: Forms | Version: dev
Severity: Normal | Keywords: widget readonly
Triage Stage: | admin
Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Hi everyone, recently I came across the following issue which already saw
some attention on StackOverflow.

All solutions I've seen so far circumvent a limitation of Django widgets:
these are not really aware of a readonly situation. There are specialized
readonly widgets for certain situations however there is no generic path
for a Widget designer to specify how a readonly version of a widget would
look like. Additionally there's is no easy way to override that behavior
(see my last response on the SO issue) in admin.

Admin, however, is just part of Django, so I would rather see a more
general approach here. Therefore, I propose the following.

Couldn't we define a ReadonlyAwareInput that allows the following:

{{{
class ReadonlyAwareInput(Input):
template_name = "django/forms/widgets/input.html"
readonly_template_name = "django/forms/widgets/readonly_input.html" #
new

def __init__(self, attrs=None, is_readonly=False):
self.is_readonly= is_readonly
if attrs is not None:
attrs = attrs.copy()
self.input_type = attrs.pop("type", self.input_type)
super().__init__(attrs)

def get_context(self, name, value, attrs):
context = super().get_context(name, value, attrs)
context["widget"]["type"] = self.input_type
context["is_readonly"] = self.is_readonly
return context


class PreviewFileInput(ReadonlyAwareInput):
template_name = "django/forms/widgets/preview_file.html"
readonly_template_name =
"django/forms/widgets/readonly_preview_file.html" # override new
readonly_template_name

def get_context(self, name, value, attrs):
.... # as usual
}}}

I am not sure if Admin's display_for_field could be replaced completely by
such a system. I my gut feeling is that it would allow customizing a lot
more.

What do you think about that solution?


Also see
- https://forum.djangoproject.com/t/feature-request-discussion-custom-
rendering-for-readonly-fields-in-admin/32009
- https://code.djangoproject.com/ticket/30577
- https://stackoverflow.com/questions/14832739/django-admin-how-to-
display-widget-on-readonly-field
--
Ticket URL: <https://code.djangoproject.com/ticket/35544>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 20, 2024, 11:44:41 AM6/20/24
to django-...@googlegroups.com
#35544: Readonly Widget Template
-------------------------------------+-------------------------------------
Reporter: Sven R. Kunze | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: dev
Severity: Normal | Resolution:
Keywords: widget readonly | Triage Stage:
admin | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Sven R. Kunze:

Old description:
New description:

Hi everyone, recently I came across the following issue which already saw
some attention on StackOverflow.

All solutions I've seen so far circumvent a limitation of Django widgets:
these are not really aware of a readonly situation. There are specialized
readonly widgets for certain situations however there is no generic path
for a Widget designer to specify how a readonly version of a widget would
look like. Additionally there's is no easy way to override that behavior
(see my last response on the SO issue) in admin.

Admin, however, is just part of Django, so I would rather see a more
general approach here. Therefore, I propose the following.

Couldn't we define a ReadonlyAwareInput that allows the following:

{{{
class ReadonlyAwareInput(Input):
template_name = "django/forms/widgets/input.html"
readonly_template_name = "django/forms/widgets/readonly_input.html" #
new

def __init__(self, attrs=None, is_readonly=False):
self.is_readonly= is_readonly
if attrs is not None:
attrs = attrs.copy()
self.input_type = attrs.pop("type", self.input_type)
super().__init__(attrs)

def get_context(self, name, value, attrs):
context = super().get_context(name, value, attrs)
context["widget"]["type"] = self.input_type
context["is_readonly"] = self.is_readonly # new for usage in
template as well
return context

def render(self, name, value, attrs=None, renderer=None):
"""Render the widget as an HTML string."""
context = self.get_context(name, value, attrs)
if self.is_readonly:
return self._render(self.readonly_template_name, context,
renderer)
return self._render(self.template_name, context, renderer)


class PreviewFileInput(ReadonlyAwareInput):
template_name = "django/forms/widgets/preview_file.html"
readonly_template_name =
"django/forms/widgets/readonly_preview_file.html" # override new
readonly_template_name

def get_context(self, name, value, attrs):
.... # as usual
}}}

I am not sure if Admin's display_for_field could be replaced completely by
such a system. I my gut feeling is that it would allow customizing a lot
more.

What do you think about that solution?


Also see
- https://forum.djangoproject.com/t/feature-request-discussion-custom-
rendering-for-readonly-fields-in-admin/32009
- https://code.djangoproject.com/ticket/30577
- https://stackoverflow.com/questions/14832739/django-admin-how-to-
display-widget-on-readonly-field

--
--
Ticket URL: <https://code.djangoproject.com/ticket/35544#comment:1>

Django

unread,
Jun 21, 2024, 10:02:22 AM6/21/24
to django-...@googlegroups.com
#35544: Readonly Widget Template
-------------------------------------+-------------------------------------
Reporter: Sven R. Kunze | Owner: nobody
Type: New feature | Status: closed
Component: Forms | Version: dev
Severity: Normal | Resolution: duplicate
Keywords: widget readonly | Triage Stage:
admin | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* resolution: => duplicate
* status: new => closed

Comment:

Hi Sven, thank you for this feature suggestion and also sharing the
related links.
For new features, we currently encourage some discussion on the forum
before accepting a ticket. Can you post your suggestion
[https://forum.djangoproject.com/t/feature-request-discussion-custom-
rendering-for-readonly-fields-in-admin/32009 on the forum post] and
discuss the idea with Ideluigi?

Going to mark this as a duplicate of #30577, but we can reopen once we
have community agreement of the feature 👍
--
Ticket URL: <https://code.djangoproject.com/ticket/35544#comment:2>

Django

unread,
Jun 23, 2024, 10:12:58 AM6/23/24
to django-...@googlegroups.com
#35544: Readonly Widget Template
-------------------------------------+-------------------------------------
Reporter: Sven R. Kunze | Owner: nobody
Type: New feature | Status: closed
Component: Forms | Version: dev
Severity: Normal | Resolution: duplicate
Keywords: widget readonly | Triage Stage:
admin | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Sven R. Kunze):

Hi Sarah, sure will do! Let's see what comes out of it. :)
--
Ticket URL: <https://code.djangoproject.com/ticket/35544#comment:3>
Reply all
Reply to author
Forward
0 new messages