NumberInput Django form fields

Listen as audio
NumberInput Django form fields

Hello Techies,

In this blog, we will check different NumberInput Django form fields(IntegerField, FloatField, DecimalField) with examples.

The fields of the form are classes themselves; They manage form data and perform validation when a form is submitted.

Syntax: FieldType(**kwargs) 

Different types of NumberInput Django form fields

IntegerField

This is an input for the integer field. It uses NumberInput as the default widget. It takes 2 optional arguments which are max_value & min_value. It Normalizes to a Python integer.

Syntax: IntegerField(**kwargs)

Example:

from django import forms

class NumberFieldForm(forms.Form):
    Number = forms.IntegerField(label='Enter Number', label_suffix=" : ", min_value=1, max_value=100, required=False,
                                 widget=forms.NumberInput(attrs={'class': 'form-control'}),
                                help_text="This value is greater than or equal to 1 & less than or equal to 100.",
                                disabled = False, error_messages={'required': "Please Enter Number."})

Let’s run the server and check the result.

Rendered HTML

NumberInput Django form fields(IntegerField)

Rendered HTML with validation

Explanation :

Field OptionsDescription
labelThe label argument allows you to specify a “Field Caption” label for this area.
label_suffixThe label_suffix argument allows you to insert some text after the field’s label.
min_valueEnsures the initial integer value.
max_valueEnsures the last integer value.
requiredBy default, each field assumes a value as required, so you need to set required = false to make it not necessary.
widgetWidget Argument lets you specify widget classes to use when presenting this field. So you can use bootstrap class or other CSS class for this field to decorate your field.
For eg. name = forms.IntegerField(widget=forms.NumberInput (attrs={‘class’: ‘form-control’}))
error_messageserror_messages argument allows you to overwrite the default error message that the field will raise. Pass the error message you want to overwrite in the dictionary with matching keys.
disabledDisabled Boolean Argument, when set to true, disables the form field using the disabled HTML attribute so that it cannot be edited by users.
stripThis field is set to true by default. This field is used to remove leading and trailing white space.

DecimalField

The Django form provides decimal fields for input of decimal numbers. The default widget for decimal numbers is NumberInput. If you provide min_value and max_value then it uses MinLengthValidator and MaxLengthValidator. Both of these arguments are optional, as well as decimal_places and max_digits ​​are also optional arguments.

Syntax: DecimalField(**kwargs)

Example:

from django import forms 
  
class NumberFieldForm(forms.Form):
 
    fees = forms.DecimalField(label='Student Fees', label_suffix=" : ", min_value=1, max_value=100, max_digits=4,
                              widget=forms.NumberInput(attrs={'class': 'form-control'}),
                              decimal_places=1, required=False,
                              help_text="This value is greater than or equal to 1 & less than or equal to 100. "
                                        "Also that there are no more than 4 digits in total.",
                              disabled = False,
                              error_messages={'required': "Please Enter Fees amount."})

Let’s run the server and check the result.

Rendered HTML

NumberInput Django form fields(DecimalField)

Rendered HTML with validation

NumberInput Django form fields(DecimalField)

Explanation :

Field OptionsDescription
labelThe label argument allows you to specify a “Field Caption” label for this area.
label_suffixThe label_suffix argument allows you to insert some text after the field’s label.
min_valueEnsures the initial decimal value.
max_valueEnsures the last decimal value.
max_digitsThe maximum number of digits allowed in the value (The leading zeros before and after the decimal point are stripped)
decimal_placesThe maximum decimal places allowed.
requiredBy default, each field assumes a value as required, so you need to set required = false to make it not necessary.
widgetWidget Argument lets you specify widget classes to use when presenting this field. So you can use bootstrap class or other CSS class for this field to decorate your field.
For eg. name = forms.DecimalField(widget=forms.NumberInput (attrs={‘class’: ‘form-control’}))
error_messageserror_messages argument allows you to overwrite the default error message that the field will raise. Pass the error message you want to overwrite in the dictionary with matching keys.
disabledDisabled Boolean Argument, when set to true, disables the form field using the disabled HTML attribute so that it cannot be edited by users.

FloatField

The FloatField in the Django form is an integer field to get the input of the floating-point number from the user. The default widget for Float numbers is NumberInput. If you provide a max_value and a min_value, they use MaxLengthValidator and MinLengthValidator. Both of these arguments are optional.

Syntax: FloatField(**kwargs)

Example:

from django import forms 
  
class NumberFieldForm(forms.Form):
    rate = forms.FloatField(label='Student Fees', label_suffix=" : ", min_value=1, max_value=100, required=False,
                            widget=forms.NumberInput(attrs={'class': 'form-control'}),
                            help_text="This value is greater than or equal to 1 & less than or equal to 100.",  disabled = False,
                            error_messages={'required': "Please Enter rate value."})

Let’s run the server and check the result.

Rendered HTML

NumberInput Django form fields(FloatField)

Rendered HTML With Validation

NumberInput Django form fields(FloatField)

Explanation:

Field OptionsDescription
labelThe label argument allows you to specify a “Field Caption” label for this area.
label_suffixThe label_suffix argument allows you to insert some text after the field’s label.
min_valueEnsures the initial Float value.
max_valueEnsures the last Float value.
requiredBy default, each field assumes a value as required, so you need to set required = false to make it not necessary.
widgetWidget argument lets you specify widget classes to use when presenting this field. So you can use bootstrap class or other CSS class for this field to decorate your field.
For eg. name = forms.FloatField(widget=forms.NumberInput(attrs={‘class’: ‘form-control’}))
error_messageserror_messages argument allows you to overwrite the default error message that the field will raise. Pass the error message you want to overwrite in the dictionary with matching keys.
disabledDisabled Boolean Argument, when set to true, disables the form field using the disabled HTML attribute so that it cannot be edited by users.

I hope you understand all the NumberInput Django form fields. For more details, you can check out the official site.

Find the source code for NumberInput Django form fields Github link at:

https://github.com/pranalikambli/django_form_fields

thank you

Leave a Comment