0% found this document useful (0 votes)
8 views

7 Django Forms

Uploaded by

rohanraj.gls0810
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

7 Django Forms

Uploaded by

rohanraj.gls0810
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Django Forms

Django provides a Form class which is used to create HTML forms. It describes a form and how it works and
appears.

It is similar to the ModelForm class that creates a form by using the Model, but it does not require the
Model.

Each field of the form class map to the HTML form <input> element and each one is a class itself, it
manages form data and performs validation while submitting the form.

Lets see an example, in which we are creating some fields too.

from django import forms


class StudentForm(forms.Form):
firstname = forms.CharField(label="Enter first name",max_length=50)
lastname = forms.CharField(label="Enter last name", max_length = 100)

A StudentForm is created that contains two fields of CharField type. Charfield is a class and used to create
an HTML text input component in the form.

The label is used to set HTML label of the component and max_length sets length of an input value.

When rendered, it produces the following HTML to the browser.

<label for="id_firstname">Enter first name:</label>


<input type="text" name="firstname" required maxlength="50" id="id_firstname" />
<label for="id_lastname">Enter last name:</label> <input type="text" name="lastname" required maxlength=

Note: Django Form does not include <form> tags, or a submit button. We'll have to provide
those ourselves in the template.

Commonly used fields and their details are given in the below table.

Name Class HTML Input Empty value

BooleanField class BooleanField(**kwargs) CheckboxInput False

CharField class CharField(**kwargs) TextInput Whatever you've given as


empty_value.

ChoiceField class ChoiceField(**kwargs) Select '' (an empty string)


DateField class DateField(**kwargs) DateInput None

DateTimeField class DateTimeInput None


DateTimeField(**kwargs)

DecimalField class DecimalField(**kwargs) NumberInput None

EmailField class EmailField(**kwargs) EmailInput '' (an empty string)

FileField class FileField(**kwargs) ClearableFileInput None

ImageField class ImageField(**kwargs) ClearableFileInput None

Let's see a complete example to create an HTML form with the help of Django Form class.

Building a Form in Django

Suppose we want to create a form to get Student information, use the following code.

from django import forms


class StudentForm(forms.Form):
firstname = forms.CharField(label="Enter first name",max_length=50)
lastname = forms.CharField(label="Enter last name", max_length = 100)

Put this code into the forms.py file.

Instantiating Form in Django

Now, we need to instantiate the form in views.py file. See, the below code.

// views.py

from django.shortcuts import render


from myapp.form import StudentForm

def index(request):
student = StudentForm()
return render(request,"index.html",{'form':student})

Passing the context of form into index template that looks like this:

// index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Index</title>
</head>
<body>
<form method="POST" class="post-form">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-default">Save</button>
</form>
</body>
</html>

Provide the URL in urls.py

from django.contrib import admin


from django.urls import path
from myapp import views
urlpatterns = [
path('admin/', admin.site.urls),
path('index/', views.index),
]

Run Server and access the form at browser by localhost:8000/index, and it will produce the following output.

There are other output options though for the <label>/<input> pairs:

◦ {{ form.as_table }} will render them as table cells wrapped in <tr> tags

◦ {{ form.as_p }} will render them wrapped in <p> tags


◦ {{ form.as_ul }} will render them wrapped in <li> tags

Note: that we'll have to provide the surrounding <table> or <ul> elements yourself.

← Prev Next →

Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

◦ Send your Feedback to [email protected]

Help Others, Please Share

Learn Latest Tutorials

Splunk SPSS tutorial Swagger T-SQL tutorial Tumblr


tutorial SPSS tutorial Transact-SQL tutorial
Splunk Swagger Tumblr

React tutorial Regex tutorial Reinforcement R RxJS tutorial


ReactJS Regex learning Programming RxJS
tutorial tutorial
Reinforcement R Programming
Learning

React Native Python Design Python Pillow Python Turtle Keras tutorial
tutorial Patterns tutorial tutorial Keras
React Native Python Design Python Pillow Python Turtle
Patterns

Preparation

You might also like