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
| Attribute | Type | Description |
|---|---|---|
| allow_multiple_selected | boolean = False | Boolean flag indicating if the widget supports selecting more than one file at once. |
| input_type | string = "file" | The HTML input type attribute used when rendering the widget. |
| needs_multipart_form | boolean = True | Boolean indicating that the parent form must use multipart encoding to handle file uploads correctly. |
| template_name | string = "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
| Name | Type | Description |
|---|---|---|
| attrs | dict = None | A 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
| Name | Type | Description |
|---|---|---|
| value | any | The current value of the field, which is ignored during rendering |
Returns
| Type | Description |
|---|---|
null | Always 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
| Name | Type | Description |
|---|---|---|
| data | dict | The dictionary of POST data |
| files | dict | The dictionary of uploaded file objects |
| name | string | The name of the HTML input element to retrieve from the files dictionary |
Returns
| Type | Description |
|---|---|
object | A 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
| Name | Type | Description |
|---|---|---|
| data | dict | The dictionary of POST data |
| files | dict | The dictionary of uploaded file objects |
| name | string | The name of the HTML input element to check for existence |
Returns
| Type | Description |
|---|---|
boolean | True 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
| Name | Type | Description |
|---|---|---|
| initial | any | The initial value or existing file associated with the field |
Returns
| Type | Description |
|---|---|
boolean | True if the field is required and no initial file is present, otherwise False |