Skip to main content

FileInput

This class represents an HTML file upload widget for forms, specifically designed to handle file data from multipart form submissions. It manages the necessary attributes for file selection and ensures that data is retrieved from the uploaded files collection rather than standard POST data. The class also provides logic to toggle support for multiple file selections based on its configuration.

Attributes

AttributeTypeDescription
allow_multiple_selectedboolean = FalseBoolean flag indicating if the widget supports selecting more than one file at once.
input_typestring = "file"The HTML input type attribute used when rendering the widget.
needs_multipart_formboolean = TrueBoolean indicating that the parent form must use multipart encoding to handle file uploads correctly.
template_namestring = "django/forms/widgets/file.html"The path to the HTML template used to render the file input widget.

Constructor

Signature

def FileInput(
attrs: dict = None
) - > null

Parameters

NameTypeDescription
attrsdict = NoneA dictionary containing HTML attributes to be set on the rendered widget.

Methods


format_value()

@classmethod
def format_value(
value: any
) - > null

File input never renders a value.

Parameters

NameTypeDescription
valueanyThe current value of the field, which is ignored during rendering

Returns

TypeDescription
nullAlways returns None as file inputs cannot have a pre-filled value for security reasons

value_from_datadict()

@classmethod
def value_from_datadict(
data: dict,
files: dict,
name: string
) - > object

File widgets take data from FILES, not POST

Parameters

NameTypeDescription
datadictThe dictionary of POST data
filesdictThe dictionary of uploaded file objects
namestringThe name of the HTML input element to retrieve from the files dictionary

Returns

TypeDescription
objectA single file object or a list of file objects if multiple selection is enabled

value_omitted_from_data()

@classmethod
def value_omitted_from_data(
data: dict,
files: dict,
name: string
) - > boolean

Checks if the file input name is missing from the uploaded files dictionary.

Parameters

NameTypeDescription
datadictThe dictionary of POST data
filesdictThe dictionary of uploaded file objects
namestringThe name of the HTML input element to check for existence

Returns

TypeDescription
booleanTrue if the specific input name is not present in the files dictionary, False otherwise

use_required_attribute()

@classmethod
def use_required_attribute(
initial: any
) - > boolean

Determines if the HTML 'required' attribute should be rendered based on whether an initial file already exists.

Parameters

NameTypeDescription
initialanyThe initial value or existing file associated with the field

Returns

TypeDescription
booleanTrue if the field is required and no initial file is present, otherwise False