class ClearableFileInput
from django.forms import ClearableFileInput
Attributes
Defined in | |
---|---|
clear_checkbox_label = <django.utils.functional.lazy.<locals>.__proxy__ object at 0x1075d3978>
|
ClearableFileInput |
initial_text = <django.utils.functional.lazy.<locals>.__proxy__ object at 0x1075d3208>
|
ClearableFileInput |
input_text = <django.utils.functional.lazy.<locals>.__proxy__ object at 0x1075d3438>
|
ClearableFileInput |
input_type = 'file'
|
FileInput |
input_type = None
|
Input |
is_localized = False
|
Widget |
is_required = False
|
Widget |
needs_multipart_form = True
|
FileInput |
needs_multipart_form = False
|
Widget |
supports_microseconds = True
|
Widget |
template_with_clear = '%(clear)s <label for="%(clear_checkbox_id)s">%(clear_checkbox_label)s</label>'
|
ClearableFileInput |
template_with_initial = '%(initial_text)s: <a href="%(initial_url)s">%(initial)s</a> %(clear_template)s<br />%(input_text)s: %(input)s'
|
ClearableFileInput |
Properties
def
is_hidden():
¶
Widget
def
media():
¶
ClearableFileInput
Getter
def _media(self):
# Get the media property of the superclass, if it exists
sup_cls = super(cls, self)
try:
base = sup_cls.media
except AttributeError:
base = Media()
# Get the media definition for this class
definition = getattr(cls, 'Media', None)
if definition:
extend = getattr(definition, 'extend', True)
if extend:
if extend is True:
m = base
else:
m = Media()
for medium in extend:
m = m + base[medium]
return m + Media(definition)
else:
return Media(definition)
else:
return base
def
media():
¶
FileInput
Getter
def _media(self):
# Get the media property of the superclass, if it exists
sup_cls = super(cls, self)
try:
base = sup_cls.media
except AttributeError:
base = Media()
# Get the media definition for this class
definition = getattr(cls, 'Media', None)
if definition:
extend = getattr(definition, 'extend', True)
if extend:
if extend is True:
m = base
else:
m = Media()
for medium in extend:
m = m + base[medium]
return m + Media(definition)
else:
return Media(definition)
else:
return base
def
media():
¶
Input
Getter
def _media(self):
# Get the media property of the superclass, if it exists
sup_cls = super(cls, self)
try:
base = sup_cls.media
except AttributeError:
base = Media()
# Get the media definition for this class
definition = getattr(cls, 'Media', None)
if definition:
extend = getattr(definition, 'extend', True)
if extend:
if extend is True:
m = base
else:
m = Media()
for medium in extend:
m = m + base[medium]
return m + Media(definition)
else:
return Media(definition)
else:
return base
def
media():
¶
Widget
Getter
def _media(self):
# Get the media property of the superclass, if it exists
sup_cls = super(cls, self)
try:
base = sup_cls.media
except AttributeError:
base = Media()
# Get the media definition for this class
definition = getattr(cls, 'Media', None)
if definition:
extend = getattr(definition, 'extend', True)
if extend:
if extend is True:
m = base
else:
m = Media()
for medium in extend:
m = m + base[medium]
return m + Media(definition)
else:
return Media(definition)
else:
return base
Methods
def
_format_value(*args, **kwargs):
¶
Input
def wrapped(*args, **kwargs):
warnings.warn(
"`%s.%s` is deprecated, use `%s` instead." %
(self.class_name, self.old_method_name, self.new_method_name),
self.deprecation_warning, 2)
return f(*args, **kwargs)
def
build_attrs(self, extra_attrs=None, **kwargs):
¶
Widget
Helper function for building an attribute dictionary.
def build_attrs(self, extra_attrs=None, **kwargs):
"Helper function for building an attribute dictionary."
attrs = dict(self.attrs, **kwargs)
if extra_attrs:
attrs.update(extra_attrs)
return attrs
def
clear_checkbox_id(self, name):
¶
ClearableFileInput
Given the name of the clear checkbox input, return the HTML id for it.
def clear_checkbox_id(self, name):
"""
Given the name of the clear checkbox input, return the HTML id for it.
"""
return name + '_id'
def
clear_checkbox_name(self, name):
¶
ClearableFileInput
Given the name of the file input, return the name of the clear checkbox input.
def clear_checkbox_name(self, name):
"""
Given the name of the file input, return the name of the clear checkbox
input.
"""
return name + '-clear'
def
format_value(self, value):
¶
Input
def format_value(self, value):
if self.is_localized:
return formats.localize_input(value)
return value
def
get_template_substitution_values(self, value):
¶
ClearableFileInput
Return value-related substitutions.
def get_template_substitution_values(self, value):
"""
Return value-related substitutions.
"""
return {
'initial': conditional_escape(value),
'initial_url': conditional_escape(value.url),
}
def
id_for_label(self, id_):
¶
Widget
Returns the HTML ID attribute of this Widget for use by a <label>, given the ID of the field. Returns None if no ID is 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.
def id_for_label(self, id_):
"""
Returns the HTML ID attribute of this Widget for use by a <label>,
given the ID of the field. Returns None if no ID is 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.
"""
return id_
def
is_initial(self, value):
¶
ClearableFileInput
Return whether value is considered to be initial value.
def is_initial(self, value):
"""
Return whether value is considered to be initial value.
"""
return bool(value and getattr(value, 'url', False))
def
render(self, name, value, attrs=None):
¶
ClearableFileInput
def render(self, name, value, attrs=None):
substitutions = {
'initial_text': self.initial_text,
'input_text': self.input_text,
'clear_template': '',
'clear_checkbox_label': self.clear_checkbox_label,
}
template = '%(input)s'
substitutions['input'] = super(ClearableFileInput, self).render(name, value, attrs)
if self.is_initial(value):
template = self.template_with_initial
substitutions.update(self.get_template_substitution_values(value))
if not self.is_required:
checkbox_name = self.clear_checkbox_name(name)
checkbox_id = self.clear_checkbox_id(checkbox_name)
substitutions['clear_checkbox_name'] = conditional_escape(checkbox_name)
substitutions['clear_checkbox_id'] = conditional_escape(checkbox_id)
substitutions['clear'] = CheckboxInput().render(checkbox_name, False, attrs={'id': checkbox_id})
substitutions['clear_template'] = self.template_with_clear % substitutions
return mark_safe(template % substitutions)
FileInput
def render(self, name, value, attrs=None):
return super(FileInput, self).render(name, None, attrs=attrs)
Input
def render(self, name, value, attrs=None):
if value is None:
value = ''
final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
if value != '':
# Only add the 'value' attribute if a value is non-empty.
final_attrs['value'] = force_text(self.format_value(value))
return format_html('<input{} />', flatatt(final_attrs))
Widget
Returns this Widget rendered as HTML, as a Unicode string. The 'value' given is not guaranteed to be valid input, so subclass implementations should program defensively.
def render(self, name, value, attrs=None):
"""
Returns this Widget rendered as HTML, as a Unicode string.
The 'value' given is not guaranteed to be valid input, so subclass
implementations should program defensively.
"""
raise NotImplementedError('subclasses of Widget must provide a render() method')
def
subwidgets(self, name, value, attrs=None, choices=()):
¶
Widget
Yields all "subwidgets" of this widget. Used only by RadioSelect to allow template access to individual <input type="radio"> buttons. Arguments are the same as for render().
def subwidgets(self, name, value, attrs=None, choices=()):
"""
Yields all "subwidgets" of this widget. Used only by RadioSelect to
allow template access to individual <input type="radio"> buttons.
Arguments are the same as for render().
"""
yield SubWidget(self, name, value, attrs, choices)
def
use_required_attribute(self, initial):
¶
ClearableFileInput
def use_required_attribute(self, initial):
return super(ClearableFileInput, self).use_required_attribute(initial) and not initial
Widget
def use_required_attribute(self, initial):
return not self.is_hidden
def
value_from_datadict(self, data, files, name):
¶
ClearableFileInput
def value_from_datadict(self, data, files, name):
upload = super(ClearableFileInput, self).value_from_datadict(data, files, name)
if not self.is_required and CheckboxInput().value_from_datadict(
data, files, self.clear_checkbox_name(name)):
if upload:
# If the user contradicts themselves (uploads a new file AND
# checks the "clear" checkbox), we return a unique marker
# object that FileField will turn into a ValidationError.
return FILE_INPUT_CONTRADICTION
# False signals to clear any existing value, as opposed to just None
return False
return upload
FileInput
File widgets take data from FILES, not POST
def value_from_datadict(self, data, files, name):
"File widgets take data from FILES, not POST"
return files.get(name)
Widget
Given a dictionary of data and this widget's name, returns the value of this widget. Returns None if it's not provided.
def value_from_datadict(self, data, files, name):
"""
Given a dictionary of data and this widget's name, returns the value
of this widget. Returns None if it's not provided.
"""
return data.get(name)
def
value_omitted_from_data(self, data, files, name):
¶
ClearableFileInput
def value_omitted_from_data(self, data, files, name):
return (
super(ClearableFileInput, self).value_omitted_from_data(data, files, name) and
self.clear_checkbox_name(name) not in data
)
FileInput
def value_omitted_from_data(self, data, files, name):
return name not in files
Widget
def value_omitted_from_data(self, data, files, name):
return name not in data