class Widget(attrs=None) [source]
This abstract class cannot be rendered, but provides the basic attribute attrs. You may also implement or override the render() method on custom widgets.
-
attrs -
A dictionary containing HTML attributes to be set on the rendered widget.
>>> from django import forms >>> name = forms.TextInput(attrs={'size': 10, 'title': 'Your name',}) >>> name.render('name', 'A name') '<input title="Your name" type="text" name="name" value="A name" size="10" required />'If you assign a value of
TrueorFalseto an attribute, it will be rendered as an HTML5 boolean attribute:>>> name = forms.TextInput(attrs={'required': True}) >>> name.render('name', 'A name') '<input name="name" type="text" value="A name" required />' >>> >>> name = forms.TextInput(attrs={'required': False}) >>> name.render('name', 'A name') '<input name="name" type="text" value="A name" />'
-
supports_microseconds -
An attribute that defaults to
True. If set toFalse, the microseconds part ofdatetimeandtimevalues will be set to0.New in Django 1.9:In older versions, this attribute was only defined on the date and time widgets (as
False).
-
format_value(value) -
Cleans and returns a value for use in the widget template.
valueisn’t guaranteed to be valid input, therefore subclass implementations should program defensively.Changed in Django 1.10:In older versions, this method is a private API named
_format_value(). The old name will work until Django 2.0.
-
id_for_label(self, id_)[source] -
Returns the HTML ID attribute of this widget for use by a
<label>, given the ID of the field. ReturnsNoneif an ID isn’t available.This hook is necessary because some widgets have multiple HTML elements and, thus, multiple IDs. In that case, this method should return an ID value that corresponds to the first ID in the widget’s tags.
-
render(name, value, attrs=None)[source] -
Returns HTML for the widget, as a Unicode string. This method must be implemented by the subclass, otherwise
NotImplementedErrorwill be raised.The ‘value’ given is not guaranteed to be valid input, therefore subclass implementations should program defensively.
-
value_from_datadict(data, files, name)[source] -
Given a dictionary of data and this widget’s name, returns the value of this widget.
filesmay contain data coming fromrequest.FILES. ReturnsNoneif a value wasn’t provided. Note also thatvalue_from_datadictmay be called more than once during handling of form data, so if you customize it and add expensive processing, you should implement some caching mechanism yourself.
-
value_omitted_from_data(data, files, name)[source] -
New in Django 1.10.2.
Given
dataandfilesdictionaries and this widget’s name, returns whether or not there’s data or files for the widget.The method’s result affects whether or not a field in a model form falls back to its default.
Special cases are
CheckboxInputandCheckboxSelectMultiple, which always returnFalsebecause an unchecked checkbox doesn’t appear in the data of an HTML form submission, so it’s unknown whether or not the user actually submitted a value.
Please login to continue.