When collecting user input in Django, forms play a crucial role in validating and processing the data before saving it to the database. Django provides a rich set of built-in form field types that correspond to different kinds of input, making it easy to build complex forms quickly.
Each form field type comes with its own validation logic and widget (HTML input type) to help gather and verify user data efficiently.
Common Django Form Field TypesTo master form handling and other Django functionalities, you can check Django Web Development Course.
Here is a summary of commonly used Django form fields, their purposes, and their default widgets:
Django Form - BooleanfieldBooleanField Represents a checkbox that stores True or False.
Default widget: CheckboxInput
Syntax:
Django Form - Charfieldfield_name = forms.BooleanField(**options)
CharField Used for short to medium text inputs.
Default widget: TextInput
Syntax:
Django Form - Choicefieldfield_name = forms.CharField(**options)
ChoiceField lets users select one option from a predefined list.
Default widget: Select
Syntax:
Django Form - Datefieldfield_name = forms.ChoiceField(**options)
DateField in Django Forms is a date field, for taking input of dates from the user.
Default widget: DateInput
Syntax:
Django Form - Datetimefieldfield_name = forms.DateField(**options)
DateTimeField in Django Forms is a date field, for taking input of date and time from the user.
Default widget: DateTimeInput
Syntax:
Django Form - Decimalfieldfield_name = forms.DateTimeField(**options)
DecimalField in Django Forms is a decimal field, for input of decimal numbers.
Default widget: NumberInput
Syntax:
Django Form - Durationfieldfield_name = forms.DecimalField(**options)
DurationField in Django Forms is used for input of particular durations for example from 12 am to 1 pm.
Default widget: TextInput
Syntax:
Django Form - Emailfieldfield_name = forms.DurationField(**options)
EmailField in Django Forms is a string field, for input of Emails. It is used for taking text inputs from the user.
Default widget: EmailInput
Syntax:
Django Form - Filefieldfield_name = forms.EmailField(**options)
FileField in Django Forms is an input field for the upload of files.
Default widget: ClearableFileInput
Syntax:
Django Form - Filepathfieldfield_name = forms.FileField(**options)
FilePathField selects a file path from the server.
Default widget: Select
Syntax:
Django Form - Floatfieldfield_name = forms.FilePathField(**options)
FloatField in Django Forms is an integer field, for taking input of floating point numbers from the user.
Default widget: NumberInput
Syntax:
Django Form - Genericipaddressfieldfield_name = forms.FloatField(**options)
GenericIPAddressField in Django Forms is a text field, for input of IP Addresses. It is a field containing either an IPv4 or an IPv6 address.
Default widget: TextInput
Syntax:
Django Form - Imagefieldfield_name = forms.GenericIPAddressField(**options)
ImageField in Django Forms is an input field for the upload of image files.
Default widget: ClearableFileInput
Syntax:
Django Form - Integerfieldfield_name = forms.ImageField(**options)
IntegerField in Django Forms is an integer field, for the input of Integer numbers.
Default widget: NumberInput
Syntax:
Django Form - Multiplechoicefieldfield_name = forms.IntegerField(**options)
MultipleChoiceField in Django Forms is a Choice field, for input of multiple pairs of values from a field.
Default widget: SelectMultiple
Syntax:
Django Form - Nullbooleanfieldfield_name = forms.MultipleChoiceField(**options)
NullBooleanField in Django Forms is a select field that stores either True or False. It is used for taking boolean inputs from the user.
Default widget: NullBooleanSelect
Syntax:
Django Form - Regexfieldfield_name = forms.NullBooleanField(**options)
RegexField in Django Forms is a string field, for small- to large-sized strings that one can match with a particular regular expression. It is used for taking selected text inputs from the user.
Default widget: TextInput
Syntax:
Django Form - Slugfieldfield_name = forms.RegexField(**options)
SlugField in Django Forms is a slug field, for input of slugs for particular URLs or similar. This field is intended for use in representing a model SlugField in forms.
Default widget: TextInput
Syntax:
Django Form - Timefieldfield_name = forms.SlugField(**options)
TimeField in Django Forms is a time input field, for input of time for a particular instance or similar.
Default widget: TimeInput
Syntax:
Django Form - Typedchoicefieldfield_name = forms.TimeField(**options)
TypedChoiceField in Django Forms is a field just like ChoiceField, for selecting a particular choice out of a list of available choices. It is used to implement State, Countries etc.
Default widget: Select
Syntax:
Django Form - Typedmultiplechoicefieldfield_name = forms.TypedChoiceField(**options)
TypedMultipleChoiceField in Django Forms is a Choice field, for input of multiple pairs of values from a field and it includes a coerce function also to convert data into specific data types.
Default widget: SelectMultiple
Syntax:
Django Form - Urlfieldfield_name = forms.TypedMultipleChoiceField(**options)
URLField in Django Forms is a URL field, for the input of URLs from a user. This field is intended for use in representing a model URLField in forms.
Default widget: URLInput
Syntax
Django Form - Uuidfieldfield_name = forms.URLField(**options)
UUIDField in Django Forms is a UUID field, for the input of UUIDs from a user.
Default widget: TextInput
Syntax:
field_name = forms.UUIDField(**options)
Read Next: Initial form data – Django Forms
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4