The Wayback Machine - https://web.archive.org/web/20240717024322/https://www.geeksforgeeks.org/how-to-create-a-form-using-django-forms/
Open In App

How to create a form using Django Forms ?

Last Updated : 16 Aug, 2021
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

Django forms are an advanced set of HTML forms that can be created using python and support all features of HTML forms in a pythonic way. This post revolves around how to create a basic form using various Form Fields and attributes. Creating a form in Django is completely similar to creating a model, one needs to specify what fields would exist in the form and of what type. For example, to input, a registration form one might need First Name (CharField), Roll Number (IntegerField), and so on.
 

Creating a form using Django Forms

Illustration of Django Forms 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. 
 

In your geeks app make a new file called forms.py where you would be making all your forms. To create a Django form you need to use Django Form Class. Let’s demonstrate it.
 

In your forms.py Enter the following, 

Python3




from django import forms
 
# creating a form
class InputForm(forms.Form):
 
    first_name = forms.CharField(max_length = 200)
    last_name = forms.CharField(max_length = 200)
    roll_number = forms.IntegerField(
                     help_text = "Enter 6 digit roll number"
                     )
    password = forms.CharField(widget = forms.PasswordInput())


Let’s explain what exactly is happening, left side denotes the name of the field and to the right of it, you define various functionalities of an input field correspondingly. A field’s syntax is denoted as 

Syntax : 

Field_name = forms.FieldType(attributes)

Now to render this form into a view, move to views.py and create a home_view as below. 

Python3




from django.shortcuts import render
from .forms import InputForm
 
# Create your views here.
def home_view(request):
    context ={}
    context['form']= InputForm()
    return render(request, "home.html", context)


In view, one needs to just create an instance of the form class created above in forms.py. Now let’s edit templates > home.html 

HTML




<form action = "" method = "post">
    {% csrf_token %}
    {{form }}
    <input type="submit" value=Submit">
</form>


All set to check if the form is working or not let’s visit http://localhost:8000/ 

create-django-form

The form is working properly but visuals are disappointing, Django provides some predefined ways to show forms in a convenient manner. In templates, the following will modify the inputs as, 

One can modify these settings also and show fields as he wants using {{ form.field_name }} but this may alter the normal process of validation if some field is empty and hence needs extraordinary care. More – Django Forms



Previous Article
Next Article

Similar Reads

Styling Django Forms with django-crispy-forms
Django by default doesn't provide any Django form styling method due to which it takes a lot of effort and precious time to beautifully style a form. django-crispy-forms solves this problem for us. It will let you control the rendering behavior of your Django forms in a very elegant and DRY way. Modules required:django : django installdjango-crispy
1 min read
How to customize Django forms using Django Widget Tweaks ?
Django forms are a great feature to create usable forms with just few lines of code. But Django doesn't easily let us edit the form for good designs. Here, we will see one of the ways to customize Django forms, So they will look according to our wish on our HTML page. Basically we will check the methods to include our own custom css, classes, or id
3 min read
{{ form.as_p }} - Render Django Forms as paragraph
Django forms are an advanced set of HTML forms that can be created using python and support all features of HTML forms in a pythonic way. Rendering Django Forms in the template may seem messy at times but with proper knowledge of Django Forms and attributes of fields, one can easily create excellent Form with all powerful features. In this article,
2 min read
{{ form.as_table }} - Render Django Forms as table
Django forms are an advanced set of HTML forms that can be created using python and support all features of HTML forms in a pythonic way. Rendering Django Forms in the template may seem messy at times but with proper knowledge of Django Forms and attributes of fields, one can easily create excellent Form with all powerful features. In this article,
2 min read
{{ form.as_ul }} - Render Django Forms as list
Django forms are an advanced set of HTML forms that can be created using python and support all features of HTML forms in a pythonic way. Rendering Django Forms in the template may seem messy at times but with proper knowledge of Django Forms and attributes of fields, one can easily create excellent Form with all powerful features. In this article,
2 min read
Initial form data - Django Forms
After creating a Django Form, if one requires some or all fields of the form be filled with some initial data, one can use functionality of Django forms to do so. It is not the same as a placeholder, but this data will be passed into the view when submitted. There are multiple methods to do this, most common being to pass the data dictionary when w
3 min read
Render HTML Forms (GET & POST) in Django
Django is often called "Batteries Included Framework" because it has a default setting for everything and has features that can help anyone develop a website rapidly. Talking about forms, In HTML, a form is a collection of elements inside &lt;form&gt;...&lt;/form&gt; that allow a visitor to do things like entering text, select options, manipulate o
5 min read
CharField - Django Forms
CharField in Django Forms is a string field, for small- to large-sized strings. It is used for taking text inputs from the user. The default widget for this input is TextInput. It uses MaxLengthValidator and MinLengthValidator if max_length and min_length are provided. Otherwise, all inputs are valid. CharField has following optional arguments: max
5 min read
BooleanField - Django Forms
BooleanField in Django Forms is a checkbox field which stores either True or False. It is used for taking boolean inputs from the user. The default widget for this input is CheckboxInput. It normalizes to: A Python True or False value. Syntax field_name = forms.BooleanField(**options) Django form BooleanField Explanation Illustration of BooleanFiel
5 min read
ChoiceField - Django Forms
ChoiceField in Django Forms is a string field, for selecting a particular choice out of a list of available choices. It is used to implement State, Countries etc. like fields for which information is already defined and user has to choose one. It is used for taking text inputs from the user. The default widget for this input is Select.It Normalizes
5 min read
Practice Tags :
three90RightbarBannerImg