How to Create a Form using Python Django Forms API ?

Published On: 24/06/2022 | Category: Django Python


Hi Dev,

This tutorial shows you how to create a form using python django forms api. you can understand a concept of how to create a form using django forms api example. if you have question about how to create a form using python django forms api integration then I will give simple example with solution. I explained simply about how to create a form using python django forms api post.

The 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

Here, this example, to input, a contact form one might need Name (CharField),Email (EmailField) Mobile Number (IntegerField).

Let's see bellow example how to create a contact form using django forms api

Step 1 : Create a Project

In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:

django-admin startproject exampleapp
Step 2 : Create a App
python3 manage.py startapp forms

Next, you need to add it in the settings.py file as follows:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'forms',
]
Step 3 : Create a FormClass

First of all in this step we will create a new file called forms.py and create a Django form you need to use Django Form Class

forms/forms.py
from django import forms

# creating a form
class InputForm(forms.Form):
    name = forms.CharField(max_length = 200)
    email = forms.EmailField(max_length=254)
    mobile_number = forms.IntegerField()

So, don't worry i will explain all about the 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

Step 4 : Creating the Views

In this step, we need to create the views for performing our contact form.Open the forms/views.py file and add:

forms/views.py
from django.shortcuts import render
from .forms import InputForm

def contact_form(request):
    context ={}
    context['form']= InputForm()
    return render(request, "form.html", context)
Step 5 : Creating Templates File

Next, we can create the forms/templates/form.html file and the add:

Django provides some predefined ways to show forms in a convenient manner. In templates, the following will modify the inputs as,

  • {{ form.as_table }} will render them as table cells wrapped in tags
  • {{ form.as_p }} will render them wrapped in <p> tags
  • {{ form.as_ul }} will render them wrapped in <li> tags
/forms/templates/home.html
<h3>How to Create a Form using Django Forms API ? - Tuts-Station.com</h3>
<form action = "" method = "post">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Submit">
</form>
Step 6 : Creating Urls

In this section, we’ll create the urls to access our views.Go to the urls.py forms/urls.py file and update it as follows:

forms/urls.py
from django.urls import path
from forms import views

urlpatterns = [
    path('contact-us', views.contact_form, name='contact_form'),
]

Next, we will require the modify the urls.py your root preoject folder lets update the file.

exampleapp/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('forms.urls')),
]
Step 7 : Run the Server

In this step, we’ll run the local development server for playing with our app without deploying it to the web.

python manage.py runserver

Next, go to the http://localhost:8000/contact-us address with a web browser.

I Hope It will help you....