FilePathField in Django Forms is a string field, for input of path of a particular file from server. It is used for select inputs from the user. One needs to specify which folders should be used in FilePathField and field displays the inputs in form of a select field. The default widget for this input is Select.
FilePathField has following required and optional arguments:
- path:- The absolute path to the directory whose contents you want listed. This directory must exist.
- recursive :- If False (the default) only the direct contents of path will be offered as choices. If True, the directory will be descended into recursively and all descendants will be listed as choices.
- match :- A regular expression pattern; only files with names matching this expression will be allowed as choices.
- allow_files :- Optional. Either True or False. Default is True. Specifies whether files in the specified location should be included. Either this or allow_folders must be True.
- allow_folders :- Optional. Either True or False. Default is False. Specifies whether folders in the specified location should be included. Either this or allow_files must be True.
field_name = forms.FilePathField(**options)
Django form FilePathField Explanation
Illustration of FilePathField using an Example. Consider a project named geeksforgeeks having an app named geeks.
Refer to the following articles to check how to create a project and an app in Django.
Enter the following code into forms.py file of geeks app.
The label argument lets you specify the “human-friendly” label for this field. This is used when the Field is displayed in a Form.
As explained in “Outputting forms as HTML” above, the default label for a Field is generated from the field name by converting all underscores to spaces and upper-casing the first letter. Specify label if that default behavior doesn’t result in an adequate label.
The initial argument lets you specify the initial value to use when rendering this Field in an unbound Form.
To specify dynamic initial data, see the Form.initial parameter.
The widget argument lets you specify a Widget class to use when rendering this Field. See Widgets for more information.
The help_text argument lets you specify descriptive text for this Field. If you provide help_text, it will be displayed next to the Field when the Field is rendered by one of the convenience Form methods (e.g., as_ul()).
Like the model field’s help_text, this value isn’t HTML-escaped in automatically-generated forms.
Here’s a full example Form that implements help_text for two of its fields. We’ve specified auto_id=False to simplify the output.
Core Field Arguments
Core field arguments are the arguments given to each field to apply some restriction or convey a particular feature to a particular field. For example, adding a required = False argument to FilePathField will allow it to be left blank by the user. Each Field class constructor takes at least these arguments. Some field classes have additional field-specific arguments, but the following must always be accepted:
Date data can be obtained using the corresponding request dictionary. If the method is GET, the data will be available in request.GET and if post, request.POST correspondingly. In the example above, we have the temp value that we can use for any purpose.
|required||By default, each Field class assumes the value is required, so to make it not required you need to set required=False|
|label||The label argument lets you specify the “human-friendly” label for this field. This is used when the Field is displayed in a Form.|
|label_suffix||The label_suffix argument lets you override the form’s label_suffix on a per-field basis.|
|widget||The widget argument lets you specify a Widget class to use when rendering this Field. See Widgets for more information.|
|help_text||The help_text argument lets you specify descriptive text for this Field. If you provide help_text, it will be displayed next to the Field when the Field is rendered by one of the convenience Form methods. |
|error_messages||The error_messages argument lets you override the default messages that the field will raise. Pass in a dictionary with keys matching the error messages you want to override.|
|validators||The validators argument lets you provide a list of validation functions for this field. |
|localize||The localize argument enables the localization of form data input, as well as the rendered output.|
|disabled.||The disabled boolean argument, when set to True, disables a form field using the disabled HTML attribute so that it won’t be editable by users. |