Django Form | Data Types and Fields
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.
To master form handling and other Django functionalities, you can check Django Web Development Course.
Common Django Form Field Types
Here is a summary of commonly used Django form fields, their purposes, and their default widgets:
Django Form - Booleanfield
BooleanField Represents a checkbox that stores True or False.
Default widget: CheckboxInput
Syntax:
field_name = forms.BooleanField(**options)
Django Form - Charfield
CharField Used for short to medium text inputs.
Default widget: TextInput
Syntax:
field_name = forms.CharField(**options)
Django Form - Choicefield
ChoiceField lets users select one option from a predefined list.
Default widget: Select
Syntax:
field_name = forms.ChoiceField(**options)
Django Form - Datefield
DateField in Django Forms is a date field, for taking input of dates from the user.
Default widget: DateInput
Syntax:
field_name = forms.DateField(**options)
Django Form - Datetimefield
DateTimeField in Django Forms is a date field, for taking input of date and time from the user.
Default widget: DateTimeInput
Syntax:
field_name = forms.DateTimeField(**options)
Django Form - Decimalfield
DecimalField in Django Forms is a decimal field, for input of decimal numbers.
Default widget: NumberInput
Syntax:
field_name = forms.DecimalField(**options)
Django Form - Durationfield
DurationField in Django Forms is used for input of particular durations for example from 12 am to 1 pm.
Default widget: TextInput
Syntax:
field_name = forms.DurationField(**options)
Django Form - Emailfield
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:
field_name = forms.EmailField(**options)
Django Form - Filefield
FileField in Django Forms is an input field for the upload of files.
Default widget: ClearableFileInput
Syntax:
field_name = forms.FileField(**options)
Django Form - Filepathfield
FilePathField selects a file path from the server.
Default widget: Select
Syntax:
field_name = forms.FilePathField(**options)
Django Form - Floatfield
FloatField in Django Forms is an integer field, for taking input of floating point numbers from the user.
Default widget: NumberInput
Syntax:
field_name = forms.FloatField(**options)
Django Form - Genericipaddressfield
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:
field_name = forms.GenericIPAddressField(**options)
Django Form - Imagefield
ImageField in Django Forms is an input field for the upload of image files.
Default widget: ClearableFileInput
Syntax:
field_name = forms.ImageField(**options)
Django Form - Integerfield
IntegerField in Django Forms is an integer field, for the input of Integer numbers.
Default widget: NumberInput
Syntax:
field_name = forms.IntegerField(**options)
Django Form - Multiplechoicefield
MultipleChoiceField in Django Forms is a Choice field, for input of multiple pairs of values from a field.
Default widget: SelectMultiple
Syntax:
field_name = forms.MultipleChoiceField(**options)
Django Form - Nullbooleanfield
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:
field_name = forms.NullBooleanField(**options)
Django Form - Regexfield
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:
field_name = forms.RegexField(**options)
Django Form - Slugfield
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:
field_name = forms.SlugField(**options)
Django Form - Timefield
TimeField in Django Forms is a time input field, for input of time for a particular instance or similar.
Default widget: TimeInput
Syntax:
field_name = forms.TimeField(**options)
Django Form - Typedchoicefield
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:
field_name = forms.TypedChoiceField(**options)
Django Form - Typedmultiplechoicefield
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:
field_name = forms.TypedMultipleChoiceField(**options)
Django Form - Urlfield
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
field_name = forms.URLField(**options)
Django Form - Uuidfield
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