Opened 3 years ago

Closed 5 months ago

#33166 closed New feature (wontfix)

Add "field" to the context when rendering widgets.

Reported by: Jacob Rief Owned by: Jacob Rief
Component: Forms Version: 4.0
Severity: Normal Keywords: form, widget, context
Cc: David Smith, Sarah Boyce Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Unsure if this shall be a feature request, but I noticed that when rendering a field label, the template's rendering context additionally contains the field- and the form-object. If however, I render the widget itself, this information is not available inside the widget's template rendering context. Apart from this inconsistency, being able to access the field- and/or the form-object, when rendering a widget is really useful.

Example:
I would like to use the form-attribute inside some <input …>-widgets, so that each field can refer to its corresponding form. See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-form for details. The form-object itself provides a name, which is used as the <form id="…" …>. Therefore each widget has to know their form's name. This information currently can not be determined inside the widget's template.

Change History (12)

comment:1 by Claude Paroz, 3 years ago

Note that form is no longer in the context after #33134.

comment:2 by Jacob Rief, 3 years ago

Well, in case of the label rendering context, the form-object is accessible through the field. However, the widget rendering context doesn't offer the field-object.

comment:3 by Mariusz Felisiak, 3 years ago

Cc: David Smith added
Summary: Missing context when rendering form widgetsAdd "field" to the context when rendering widgets.
Triage Stage: UnreviewedAccepted
Type: Cleanup/optimizationNew feature

Tentatively accepted, this will require passing field to the Widget.render().

This is not strictly related with 456466d932830b096d39806e291fe23ec5ed38d5.

comment:4 by Jacob Rief, 3 years ago

Has patch: set
Owner: changed from nobody to Jacob Rief
Status: newassigned

Here is a draft pull request to implement this:
https://github.com/django/django/pull/14955

comment:5 by Mariusz Felisiak, 3 years ago

Patch needs improvement: set

comment:6 by David Smith, 3 years ago

I wonder about creating a circular context here. A field has a widget and you can access the field from the context of the widget and so on. See #33134 for a recent bug with field labels.

comment:7 by Mariusz Felisiak, 14 months ago

Patch needs improvement: unset

This should be review again.

comment:8 by Mariusz Felisiak, 14 months ago

Cc: Sarah Boyce added

comment:9 by Carlton Gibson, 14 months ago

As per David's comment on the PR here, we should resolve #34077 first, to see if that gives everything needed here.

comment:10 by Mariusz Felisiak, 13 months ago

Patch needs improvement: set

Marking as "Patch needs improvement" as this is waiting for #34077.

comment:11 by Mariusz Felisiak, 11 months ago

Jacob, Have you seen cad376f844c7bdeeee7607a7c0ea8ae52061309b? Do we still need this change?

comment:12 by Mariusz Felisiak, 5 months ago

Has patch: unset
Patch needs improvement: unset
Resolution: wontfix
Status: assignedclosed
Triage Stage: AcceptedUnreviewed

I think it's no longer needed after cad376f844c7bdeeee7607a7c0ea8ae52061309b.

Note: See TracTickets for help on using tickets.
Back to Top