#35192: Support injecting custom context into widget templates
------------------------------------------------+------------------------
Reporter: oxan | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Forms | Version: 5.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------------+------------------------
The context available to widget templates is currently very rigid and does
not allow any customization. For example, it's currently almost impossible
to add an `invalid` CSS class to widgets that have errors.
I think my preferred solution would be to insert a call to a form renderer
method in the callchain between `BoundField.as_widget()` and
`Widget.render()`, which can be overridden by a custom renderer. It should
receive the `BoundField` itself as an argument so that it has access to
the current state. Something like the following sketch:
{{{#!python
class BoundField:
def as_widget(self, widget=None, attrs=None, only_initial=False):
... # leave current code as-is, except for last call to
widget.render()
return self.form.renderer.render_widget(
field=self,
widget=widget,
name=self.html_initial_name if only_initial else
self.html_name,
value=value,
attrs=attrs,
)
class BaseRenderer:
def render_widget(self, field, widget, name, value, attrs=None):
# this can be overridden to add/change/remove `attrs`, or to
render the widget in an entirely different way
return widget.render(name, value, attrs)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35192>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/0107018da3321a32-1f56b4f8-6da1-471c-9e7e-a6aa7866e9df-000000%40eu-central-1.amazonses.com.