MCPcopy
hub / github.com/django/django / as_widget

Method as_widget

django/forms/boundfield.py:85–113  ·  view source on GitHub ↗

Render the field by rendering the passed widget, adding any HTML attributes passed as attrs. If a widget isn't specified, use the field's default widget.

(self, widget=None, attrs=None, only_initial=False)

Source from the content-addressed store, hash-verified

83 return {"field": self}
84
85 def as_widget(self, widget=None, attrs=None, only_initial=False):
86 """
87 Render the field by rendering the passed widget, adding any HTML
88 attributes passed as attrs. If a widget isn't specified, use the
89 field's default widget.
90 """
91 widget = widget or self.field.widget
92 if self.field.localize:
93 widget.is_localized = True
94 attrs = attrs or {}
95 attrs = self.build_widget_attrs(attrs, widget)
96 if self.auto_id and "id" not in widget.attrs:
97 attrs.setdefault(
98 "id", self.html_initial_id if only_initial else self.auto_id
99 )
100 if only_initial and self.html_initial_name in self.form.data:
101 # Propagate the hidden initial value.
102 value = self.form._widget_data_value(
103 self.field.hidden_widget(),
104 self.html_initial_name,
105 )
106 else:
107 value = self.value()
108 return widget.render(
109 name=self.html_initial_name if only_initial else self.html_name,
110 value=value,
111 attrs=attrs,
112 renderer=self.form.renderer,
113 )
114
115 def as_text(self, attrs=None, **kwargs):
116 """

Callers 5

as_textMethod · 0.95
as_textareaMethod · 0.95
as_hiddenMethod · 0.95

Calls 5

build_widget_attrsMethod · 0.95
valueMethod · 0.95
_widget_data_valueMethod · 0.80
setdefaultMethod · 0.45
renderMethod · 0.45