Django Tutorial
Django Create A Complete Project
Django Template
Django Database Query
Django Form
Django Authentication and Permission Management
Django unittest
Django Advanced
Django's Form API provides a variety of built-in form fields to handle different types of data input. In this tutorial, we'll go through some of the most common form fields and their attributes.
CharField
is used for text input and has a few optional attributes:
max_length
: The maximum length of the input.min_length
: The minimum length of the input.strip
: If set to True
(default), leading and trailing whitespaces are removed.Example:
name = forms.CharField(max_length=100)
IntegerField
is used for integer input and has a few optional attributes:
max_value
: The maximum allowed value.min_value
: The minimum allowed value.Example:
age = forms.IntegerField(min_value=18, max_value=120)
DateField
is used for date input and has a few optional attributes:
input_formats
: A list of formats for parsing the input.initial
: The initial value of the field.Example:
birthday = forms.DateField(input_formats=['%Y-%m-%d'])
BooleanField
is used for boolean input (checkbox). There are no specific attributes for this field.
Example:
is_active = forms.BooleanField()
ChoiceField
is used for a selection of choices. It has one required attribute:
choices
: A list of tuples with each tuple containing two items, the value, and the display label.Example:
GENDER_CHOICES = ( ('M', 'Male'), ('F', 'Female'), ) gender = forms.ChoiceField(choices=GENDER_CHOICES)
EmailField
is used for email input. It inherits all attributes from CharField
.
Example:
email = forms.EmailField()
URLField
is used for URL input. It inherits all attributes from CharField
.
Example:
website = forms.URLField()
FileField
is used for file input. It has a few optional attributes:
max_length
: The maximum length of the file path.allow_empty_file
: If set to True
, allows empty files to be uploaded.Example:
document = forms.FileField()
ImageField
is used for image input. It inherits all attributes from FileField
and has an additional attribute:
required
: If set to True
(default), the field must be filled in.Example:
avatar = forms.ImageField(required=False)
ModelChoiceField
is used for selecting an object from a queryset. It has a required attribute:
queryset
: The queryset to use as choices.Example:
from .models import Author author = forms.ModelChoiceField(queryset=Author.objects.all())
This tutorial covered some of the most common Django built-in form fields.
List of built-in form fields in Django:
# forms.py in your app from django import forms class MyForm(forms.Form): char_field = forms.CharField() integer_field = forms.IntegerField() date_field = forms.DateField() # ... (other fields)
Django CharField vs TextField in forms:
# forms.py in your app from django import forms class MyForm(forms.Form): char_field = forms.CharField(max_length=100) text_field = forms.CharField(widget=forms.Textarea)
Using Django IntegerField in forms:
# forms.py in your app from django import forms class MyForm(forms.Form): integer_field = forms.IntegerField()
Django DateField and TimeField in forms:
# forms.py in your app from django import forms class MyForm(forms.Form): date_field = forms.DateField() time_field = forms.TimeField()
Django EmailField in forms usage:
# forms.py in your app from django import forms class MyForm(forms.Form): email_field = forms.EmailField()
Working with Django URLField in forms:
# forms.py in your app from django import forms class MyForm(forms.Form): url_field = forms.URLField()
Handling choices with Django ChoiceField:
# forms.py in your app from django import forms class MyForm(forms.Form): gender_choices = [('M', 'Male'), ('F', 'Female')] gender_field = forms.ChoiceField(choices=gender_choices)
Django FileField and ImageField in forms:
# forms.py in your app from django import forms class MyForm(forms.Form): file_field = forms.FileField() image_field = forms.ImageField()
Using Django FloatField and DecimalField in forms:
# forms.py in your app from django import forms class MyForm(forms.Form): float_field = forms.FloatField() decimal_field = forms.DecimalField()
Customizing labels and help text in Django forms:
# forms.py in your app from django import forms class MyForm(forms.Form): name = forms.CharField(label='Your Full Name', help_text='Enter your full name.')
Django SlugField in forms examples:
# forms.py in your app from django import forms class MyForm(forms.Form): slug_field = forms.SlugField()
Django form validation with RegexField:
# forms.py in your app from django import forms class MyForm(forms.Form): postal_code = forms.RegexField(regex=r'^\d{5}$', error_messages={'invalid': 'Enter a valid 5-digit postal code.'})
Django URL validation with URLField:
# forms.py in your app from django import forms class MyForm(forms.Form): website_url = forms.URLField()
Django form handling for DateTimeField:
# forms.py in your app from django import forms class MyForm(forms.Form): meeting_datetime = forms.DateTimeField()
Django ModelChoiceField in forms:
# forms.py in your app from django import forms from .models import Category class MyForm(forms.Form): category = forms.ModelChoiceField(queryset=Category.objects.all())
Working with Django TimeField in forms:
# forms.py in your app from django import forms class MyForm(forms.Form): meeting_time = forms.TimeField()