required – Django Form Field Validation

13 Feb, 2020
Built-in Form Field Validations in Django Forms are the default validations that come predefined to all fields. Every field comes in with some built-in validations from Django validators. Each Field class constructor takes some fixed arguments. Some Field classes take additional, field-specific arguments, but required should always be accepted.
required is often used to make the field optional that is the user would no longer be required to enter the data into that field and it will still be accepted. Let’s check how to user required in a field using a project.
Syntax
field_name = models.Field(option = value)
Illustration of required 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.
How to Create a Basic Project using MVT in Django?
How to Create an App in Django ?
Enter the following code into forms.py file of geeks app. We will be using CharField for experimenting for all field options.
from django import forms  class GeeksForm(forms.Form):    geeks_field = forms.CharField(required = False)
Add the geeks app to INSTALLED_APPS
# Application definition  INSTALLED_APPS = [    'django.contrib.admin',    'django.contrib.auth',    'django.contrib.contenttypes',    'django.contrib.sessions',    'django.contrib.messages',    'django.contrib.staticfiles',    'geeks',]
Now to render this form into a view we need a view and a URL mapped to that view. Let’s create a view first in views.py of geeks app,
from django.shortcuts import renderfrom .forms import GeeksForm  # Create your views here.def home_view(request):    context = {}    form = GeeksForm(request.POST or None)    context['form'] = form    if request.POST:        if form.is_valid():            temp = form.cleaned_data.get("geeks_field")            print(temp)    return render(request, "home.html", context)
Here we are importing that particular form from forms.py and creating an object of it in the view so that it can be rendered in a template.Now, to initiate a Django form you need to create home.html where one would be designing the stuff as they like. Let’s create a form in home.html.
<form method = "POST">    {% csrf_token %}    {{ form }}    <input type = "submit" value = "Submit"></form>
Finally, a URL to map to this view in urls.py
from django.urls import path  # importing views from views..pyfrom .views import home_view  URLpatterns = [    path('', home_view ),]
Let’s run the server and check what has actually happened, Run
Python manage.py runserver
Thus, an geeks_field CharField is created by replacing “_” with ” “.
required is often used to make the field optional that is the user would no longer be required to enter the data into that field and it will still be accepted. Let’s check how to use required in a field using a project. When set to a particular value the option used appends some validations to the field as required by the developer.
Let’s try to use required via Django Web application we created, visit http://localhost:8000/ and try to input the value based on option or validation applied on the Field. Hit submit.
Hence Field is accepting the form even without any data in the geeks_field. This makes required=False implemented successfully.
NaveenArora
Django-forms
Python Django
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.