﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
35192	Support injecting custom context into widget templates	Oxan van Leeuwen	Christophe Henry	"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)
}}}
"	Cleanup/optimization	closed	Forms	5.0	Normal	needsinfo		David Smith Oxan van Leeuwen Christophe Henry	Unreviewed	1	0	0	0	0	0
