SlideShare a Scribd company logo
Django Forms
just the tip of the ice burg
Objectives
 Explore Django forms API
 Review the Django forms documentation
 Create forms using the Django Forms API
Out of the Box
 Form handling django library
 Automatically displays HTML form elements
 Validates submitted data
 Displays validation errors
 Automatically converts submitted data into relevant data
types.
Getting Started
1. forms.py
2. template rendering
3. models.py
4. views.py
forms.py
Forms are composed of fields
from django import forms
class ContactForm(forms.Form):
name = forms.CharField()
email = forms.EmailField()
age = forms.IntegerField()
forms.py - fields
Each field has custom validation logic and other useful hooks.
name = forms.CharField(
label =“Full Name”,
max_length = “50”,
)
email = forms.EmailField(
max_length = “100”,
required = “false”
)
age = forms.IntegerField(
min_value = 18,
)
forms.py - widgets
Each field has a default widget which serves to render html.
name = forms.CharField(
label =“Full Name”,
max_length = “50”,
widget = forms.TextInput(
attrs = { „size‟ : ‟50‟, „class‟ = „customClass‟ }
)
)
email = forms.EmailField(
max_length = “100”,
required = “false”,
widget = forms.HiddenInput()
)
age = forms.IntegerField(
min_value = 18,
widget = forms.TextInput()
error_messages={‟invalid': ‟Only numbers please.'}
)
Instantiation : Bound vs Unbound
Unbound forms don‟t have data associated with them, but then
can be rendered.
form = ContactForm()
Bound forms have specific data associated, which can be
validated.
form = ContactForm(data = dataDict)
Initial Data
 Data displayed only in unbound forms, and are not used as
fallback values if a particular value isn‟t provided.
Initial != Default
 Can be set two different ways :
1. Set it in the field declaration of your form.
age = forms.IntegerField(initial=18)
2. Set it when you instantiate your form.
form = ContactForm(initial = initialDict)
Form Validation Process
form.is_valid()
form._get_errors()
form.full_clean()
form._clean_fields()
for field in form.fields:
value = widget.value_from_datadict(data, files, name)
field.clean(value)
field.to_python(value)
field.validate(value)
field.run_validators(value)
field.clean_fieldname()
form._clean_form()
form.clean()
Form validation
Widget
Field
form
Form Validation Overview
 Only bound forms may be validated
 Validation is triggered when form.is_valid() is called
 Validated, cleaned data is stored in form.cleaned_data
 Perform the full validation cycle by calling form.full_clean()
Field Validating
 Three phases for fields: To Python, Validation, and Cleaning
 If validation raises an Error, cleaning is skipped
 Validators are callables that can raise a ValidationError
 Django inlcludes generic ones for common tasks.
 URLS, Min/Max Value, Email, etc.
Validators - defining
A callable that takes a value and raises a ValidationError; can
be useful for reusing validation logic.
App/validators.py
from django.core.exceptions import ValidationError
Import re
def validate_full_name(value):
if len(re.findall(r'w+', value)) < 2:
raise ValidationError(u‟%s is not a full name.‟ % value)
Validators - using
from django import forms
from projectgroup.project.application.validators import validate_full_name
class ContactForm(forms.Form):
name = forms.CharField(
label =“Full Name”,
max_length = “50”,
validators = [validate_full_name]
)
email = forms.EmailField(
max_length = “100”,
required = “false”
)
age = forms.IntegerField(
min_value = 18,
)
Field Cleaning
 .clean_fieldname() is called after validators
 Input has already been converted to Python objects
 Methods can still raise ValidationError
 Methods must return the cleaned_value
Specific Field Cleaning Example
from django import forms
from projectgroup.project.application.validators import validate_full_name
class ContactForm(forms.Form):
# Everything as before
…
def clean_email(self):
data = self.cleaned_data.get(„email‟, „‟)
if „gmail.com‟ not in data:
raise forms.ValidationError(“Why you no like gmail?”)
# always return the cleaned data, whether you have change
# it or not
return data
Form Validation
 .clean() perform cross-field validation
 Called even if errors were raised by Fields
 Must return the cleaned data dictionary
 ValidationError‟s raised by .clean() will be grouped in
form.non_field_errors() by default
Cross-field Cleaning Example
from django import forms
from projectgroup.project.application.validators import validate_full_name
class ContactForm(forms.Form):
# Everything as before
…
def clean (self):
cleaned_data = super(ContactForm, self).clean()
age = cleaned_data.get(“age”)
email = cleaned_data.get(“email”)
if not age and not email:
raise forms.ValidationError(“Age and email are required.”)
return cleaned_data
Rendering : Primary Modes
There are three primary output modes :
1. form.as_p()
<p>
<label for=“name”>Name</label>
<input type=“text” id=“name” name=“name” size=“50”/>
</p>
<p>
<label for=“email”>Email</label>
<input type=“text” id=“email” name=“email” size=“50”/>
</p>
<p>
<label for=“age”>Age</label>
<input type=“text” id=“age” name=“age” size=“3”/>
</p>
2. form.as_ul()
3. form.as_table()
Rendering : Controlled Output
{{ field.non_form_errors }}
{% for field in form %}
<div class=“fieldWrapper”>
{{ field.errors }}
{{ field.label_tag }}: {{ field }}
</div>
{% endfor %}
Manually Created
<form id=“contact” action=“” method=“get”>
<p>
<label for=“name”>Name</label>
<input type=“text” id=“name” name=“name” size=“50”/>
</p>
<p>
<label for=“email”>Email</label>
<input type=“text” id=“email” name=“email” size=“50”/>
</p>
<p>
<label for=“age”>Age</label>
<input type=“text” id=“age” name=“age” size=“3”/>
</p>
<input type=“submit” name=“save”/>
</form>
Dynamically Created
<form id=“contact” action=“” method=“get”>
{{ form.as_p }}
<input type=“submit” name=“save”/>
</form>
<form id=“contact” action=“” method=“get”>
{{ field.non_form_errors }}
{% for field in form %}
<div class=“fieldWrapper”>
{{ field.errors }}
{{ field.label_tag }}: {{ field }}
</div>
{% endfor %}
<input type=“submit” name=“save”/>
</form>
Model Forms
 modelForms map a model to a form
 validation includes model validators
 Supports creating and editing instances
 Key differences from forms
 There will be a field for a primary key (id)
 New save() method
 New instance attribute
Model Form Example
app/models.py
from django import models
class Contact(models.Model):
name = models.CharField(max_length=50)
email = models.EmailField()
age = models.IntegerField()
app/forms.py
from django import forms
from grp.prj.app.models import Contact
class ContactForm(forms.Modelform):
class Meta:
model = Contact
Limiting Fields
 Specify fields to expose, or fields to exclude
app/forms.py
from django import forms
from grp.prj.app.models import Contact
class ContactForm(forms.ModelForm):
class Meta:
model = Contact
fields = ('name', 'email',)
class ContactForm(forms.ModelForm):
class Meta:
model = Contact
exclude = (‟age',)
Instantiating Model Forms
 Like before, we may have bound and unbound Model Forms
Unbound
mform = ContactForm()
Bound
mform = ContactForm(instance=Contact.objects.get(id=2) )
Handling forms in views.py
1. Import your form(s)
2. Within your view function
1. Check for submission(GET or POST).
1. If submitted
1. bind the form to the data.
2. Validate.
1. If its valid, do stuff.
2. Otherwise let it remain unbound.
3. Pass form to the context
4. Render your page.
views.py example
from grp.prj.app.forms import ContactForm
…
def contactView(request):
context = {}
contactPDA = Dpdpcntc()
…
initials = {
‘age' : ’18'
}
#instantiate
if request.GET.get(„submit‟):
form = ContactForm(data = request.GET)
if form.is_valid():
….
else:
form = ContactForm(initial=initials)
context[„form‟] = form
return render_to_response(…)
Import Form
Check for submission
Bind Form
Form Validation
Unbound Form
Render HTML
Form in Context
views.py – other examples
if request.GET.get(„submit‟):
form = ContactForm(request.GET)
if form.is_valid():
….
else:
form = ContactForm(initial=initials)
if request.GET:
form = ContactForm(request.GET)
if form.is_valid():
….
else:
form = ContactForm(initial=initials)
form = ContactForm(data=request.GET or None, initial=initials)
if form.is_valid():
….
Fin
Questions?

More Related Content

What's hot

A Basic Django Introduction
A Basic Django IntroductionA Basic Django Introduction
A Basic Django IntroductionGanga Ram
 
Le Wagon - UI components design
Le Wagon - UI components designLe Wagon - UI components design
Le Wagon - UI components design
Boris Paillard
 
Django for Beginners
Django for BeginnersDjango for Beginners
Django for Beginners
Jason Davies
 
Le Wagon - 2h Landing
Le Wagon - 2h LandingLe Wagon - 2h Landing
Le Wagon - 2h Landing
Boris Paillard
 
jQuery PPT
jQuery PPTjQuery PPT
jQuery PPT
Dominic Arrojado
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
James Casey
 
Django
DjangoDjango
Django
Kangjin Jun
 
Techical Workflow for a Startup
Techical Workflow for a StartupTechical Workflow for a Startup
Techical Workflow for a Startup
Sébastien Saunier
 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2
fishwarter
 
Php forms
Php formsPhp forms
Php forms
Anne Lee
 
Node.js Express
Node.js  ExpressNode.js  Express
Node.js Express
Eyal Vardi
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
Php with MYSQL Database
Php with MYSQL DatabasePhp with MYSQL Database
Php with MYSQL Database
Computer Hardware & Trouble shooting
 
Php.ppt
Php.pptPhp.ppt
Php.ppt
Nidhi mishra
 
HTML
HTMLHTML
Angular 8
Angular 8 Angular 8
Angular 8
Sunil OS
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
Manvendra Singh
 
Html5 tutorial for beginners
Html5 tutorial for beginnersHtml5 tutorial for beginners
Html5 tutorial for beginners
Singsys Pte Ltd
 

What's hot (20)

A Basic Django Introduction
A Basic Django IntroductionA Basic Django Introduction
A Basic Django Introduction
 
Le Wagon - UI components design
Le Wagon - UI components designLe Wagon - UI components design
Le Wagon - UI components design
 
Django for Beginners
Django for BeginnersDjango for Beginners
Django for Beginners
 
Le Wagon - 2h Landing
Le Wagon - 2h LandingLe Wagon - 2h Landing
Le Wagon - 2h Landing
 
jQuery PPT
jQuery PPTjQuery PPT
jQuery PPT
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
 
Django
DjangoDjango
Django
 
Techical Workflow for a Startup
Techical Workflow for a StartupTechical Workflow for a Startup
Techical Workflow for a Startup
 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2
 
Php forms
Php formsPhp forms
Php forms
 
Node.js Express
Node.js  ExpressNode.js  Express
Node.js Express
 
Document Object Model
Document Object ModelDocument Object Model
Document Object Model
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
 
HTML Fundamentals
HTML FundamentalsHTML Fundamentals
HTML Fundamentals
 
Php with MYSQL Database
Php with MYSQL DatabasePhp with MYSQL Database
Php with MYSQL Database
 
Php.ppt
Php.pptPhp.ppt
Php.ppt
 
HTML
HTMLHTML
HTML
 
Angular 8
Angular 8 Angular 8
Angular 8
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
 
Html5 tutorial for beginners
Html5 tutorial for beginnersHtml5 tutorial for beginners
Html5 tutorial for beginners
 

Similar to Django forms

XPages Workshop: Concepts And Exercises
XPages Workshop:   Concepts And ExercisesXPages Workshop:   Concepts And Exercises
XPages Workshop: Concepts And Exercises
ddrschiw
 
Django Forms: Best Practices, Tips, Tricks
Django Forms: Best Practices, Tips, TricksDjango Forms: Best Practices, Tips, Tricks
Django Forms: Best Practices, Tips, Tricks
Shawn Rider
 
How to Import data into OpenERP V7
How to Import data into OpenERP V7How to Import data into OpenERP V7
How to Import data into OpenERP V7
Audaxis
 
The Django Book - Chapter 7 forms
The Django Book - Chapter 7 formsThe Django Book - Chapter 7 forms
The Django Book - Chapter 7 forms
Vincent Chien
 
Wheels
WheelsWheels
Wheels
guest9fd0a95
 
Soa lab
Soa lab   Soa lab
.NET Portfolio
.NET Portfolio.NET Portfolio
.NET Portfolio
larel
 
.NET Portfolio
.NET Portfolio.NET Portfolio
.NET Portfolio
guest1720d9
 
Forms, Getting Your Money's Worth
Forms, Getting Your Money's WorthForms, Getting Your Money's Worth
Forms, Getting Your Money's Worth
Alex Gaynor
 
JavaScript - Chapter 14 - Form Handling
 JavaScript - Chapter 14 - Form Handling   JavaScript - Chapter 14 - Form Handling
JavaScript - Chapter 14 - Form Handling
WebStackAcademy
 
Pruebas unitarias con django
Pruebas unitarias con djangoPruebas unitarias con django
Pruebas unitarias con django
Tomás Henríquez
 
Django Search
Django SearchDjango Search
Django Search
Peter Herndon
 
Advanced Django
Advanced DjangoAdvanced Django
Advanced Django
Simon Willison
 
Web design - Working with forms in HTML
Web design - Working with forms in HTMLWeb design - Working with forms in HTML
Web design - Working with forms in HTML
Mustafa Kamel Mohammadi
 
Custom Signals for Uncoupled Design
Custom Signals for Uncoupled DesignCustom Signals for Uncoupled Design
Custom Signals for Uncoupled Design
ecomsmith
 
Form Validation in JavaScript
Form Validation in JavaScriptForm Validation in JavaScript
Form Validation in JavaScript
Ravi Bhadauria
 
A multi submission importer for easyform
A multi submission importer for easyformA multi submission importer for easyform
A multi submission importer for easyform
Annette Lewis
 
Lo nuevo de Django 1.7 y 1.8
Lo nuevo de Django 1.7 y 1.8Lo nuevo de Django 1.7 y 1.8
Lo nuevo de Django 1.7 y 1.8
pedroburonv
 

Similar to Django forms (20)

XPages Workshop: Concepts And Exercises
XPages Workshop:   Concepts And ExercisesXPages Workshop:   Concepts And Exercises
XPages Workshop: Concepts And Exercises
 
Django Forms: Best Practices, Tips, Tricks
Django Forms: Best Practices, Tips, TricksDjango Forms: Best Practices, Tips, Tricks
Django Forms: Best Practices, Tips, Tricks
 
How to Import data into OpenERP V7
How to Import data into OpenERP V7How to Import data into OpenERP V7
How to Import data into OpenERP V7
 
The Django Book - Chapter 7 forms
The Django Book - Chapter 7 formsThe Django Book - Chapter 7 forms
The Django Book - Chapter 7 forms
 
Wheels
WheelsWheels
Wheels
 
Soa lab
Soa lab   Soa lab
Soa lab
 
.NET Portfolio
.NET Portfolio.NET Portfolio
.NET Portfolio
 
.NET Portfolio
.NET Portfolio.NET Portfolio
.NET Portfolio
 
Forms, Getting Your Money's Worth
Forms, Getting Your Money's WorthForms, Getting Your Money's Worth
Forms, Getting Your Money's Worth
 
JavaScript - Chapter 14 - Form Handling
 JavaScript - Chapter 14 - Form Handling   JavaScript - Chapter 14 - Form Handling
JavaScript - Chapter 14 - Form Handling
 
Pruebas unitarias con django
Pruebas unitarias con djangoPruebas unitarias con django
Pruebas unitarias con django
 
Django Search
Django SearchDjango Search
Django Search
 
Django (Web Konferencia 2009)
Django (Web Konferencia 2009)Django (Web Konferencia 2009)
Django (Web Konferencia 2009)
 
Chapter09
Chapter09Chapter09
Chapter09
 
Advanced Django
Advanced DjangoAdvanced Django
Advanced Django
 
Web design - Working with forms in HTML
Web design - Working with forms in HTMLWeb design - Working with forms in HTML
Web design - Working with forms in HTML
 
Custom Signals for Uncoupled Design
Custom Signals for Uncoupled DesignCustom Signals for Uncoupled Design
Custom Signals for Uncoupled Design
 
Form Validation in JavaScript
Form Validation in JavaScriptForm Validation in JavaScript
Form Validation in JavaScript
 
A multi submission importer for easyform
A multi submission importer for easyformA multi submission importer for easyform
A multi submission importer for easyform
 
Lo nuevo de Django 1.7 y 1.8
Lo nuevo de Django 1.7 y 1.8Lo nuevo de Django 1.7 y 1.8
Lo nuevo de Django 1.7 y 1.8
 

Django forms

  • 1. Django Forms just the tip of the ice burg
  • 2. Objectives  Explore Django forms API  Review the Django forms documentation  Create forms using the Django Forms API
  • 3. Out of the Box  Form handling django library  Automatically displays HTML form elements  Validates submitted data  Displays validation errors  Automatically converts submitted data into relevant data types.
  • 4. Getting Started 1. forms.py 2. template rendering 3. models.py 4. views.py
  • 5. forms.py Forms are composed of fields from django import forms class ContactForm(forms.Form): name = forms.CharField() email = forms.EmailField() age = forms.IntegerField()
  • 6. forms.py - fields Each field has custom validation logic and other useful hooks. name = forms.CharField( label =“Full Name”, max_length = “50”, ) email = forms.EmailField( max_length = “100”, required = “false” ) age = forms.IntegerField( min_value = 18, )
  • 7. forms.py - widgets Each field has a default widget which serves to render html. name = forms.CharField( label =“Full Name”, max_length = “50”, widget = forms.TextInput( attrs = { „size‟ : ‟50‟, „class‟ = „customClass‟ } ) ) email = forms.EmailField( max_length = “100”, required = “false”, widget = forms.HiddenInput() ) age = forms.IntegerField( min_value = 18, widget = forms.TextInput() error_messages={‟invalid': ‟Only numbers please.'} )
  • 8. Instantiation : Bound vs Unbound Unbound forms don‟t have data associated with them, but then can be rendered. form = ContactForm() Bound forms have specific data associated, which can be validated. form = ContactForm(data = dataDict)
  • 9. Initial Data  Data displayed only in unbound forms, and are not used as fallback values if a particular value isn‟t provided. Initial != Default  Can be set two different ways : 1. Set it in the field declaration of your form. age = forms.IntegerField(initial=18) 2. Set it when you instantiate your form. form = ContactForm(initial = initialDict)
  • 10. Form Validation Process form.is_valid() form._get_errors() form.full_clean() form._clean_fields() for field in form.fields: value = widget.value_from_datadict(data, files, name) field.clean(value) field.to_python(value) field.validate(value) field.run_validators(value) field.clean_fieldname() form._clean_form() form.clean() Form validation Widget Field form
  • 11. Form Validation Overview  Only bound forms may be validated  Validation is triggered when form.is_valid() is called  Validated, cleaned data is stored in form.cleaned_data  Perform the full validation cycle by calling form.full_clean()
  • 12. Field Validating  Three phases for fields: To Python, Validation, and Cleaning  If validation raises an Error, cleaning is skipped  Validators are callables that can raise a ValidationError  Django inlcludes generic ones for common tasks.  URLS, Min/Max Value, Email, etc.
  • 13. Validators - defining A callable that takes a value and raises a ValidationError; can be useful for reusing validation logic. App/validators.py from django.core.exceptions import ValidationError Import re def validate_full_name(value): if len(re.findall(r'w+', value)) < 2: raise ValidationError(u‟%s is not a full name.‟ % value)
  • 14. Validators - using from django import forms from projectgroup.project.application.validators import validate_full_name class ContactForm(forms.Form): name = forms.CharField( label =“Full Name”, max_length = “50”, validators = [validate_full_name] ) email = forms.EmailField( max_length = “100”, required = “false” ) age = forms.IntegerField( min_value = 18, )
  • 15. Field Cleaning  .clean_fieldname() is called after validators  Input has already been converted to Python objects  Methods can still raise ValidationError  Methods must return the cleaned_value
  • 16. Specific Field Cleaning Example from django import forms from projectgroup.project.application.validators import validate_full_name class ContactForm(forms.Form): # Everything as before … def clean_email(self): data = self.cleaned_data.get(„email‟, „‟) if „gmail.com‟ not in data: raise forms.ValidationError(“Why you no like gmail?”) # always return the cleaned data, whether you have change # it or not return data
  • 17. Form Validation  .clean() perform cross-field validation  Called even if errors were raised by Fields  Must return the cleaned data dictionary  ValidationError‟s raised by .clean() will be grouped in form.non_field_errors() by default
  • 18. Cross-field Cleaning Example from django import forms from projectgroup.project.application.validators import validate_full_name class ContactForm(forms.Form): # Everything as before … def clean (self): cleaned_data = super(ContactForm, self).clean() age = cleaned_data.get(“age”) email = cleaned_data.get(“email”) if not age and not email: raise forms.ValidationError(“Age and email are required.”) return cleaned_data
  • 19. Rendering : Primary Modes There are three primary output modes : 1. form.as_p() <p> <label for=“name”>Name</label> <input type=“text” id=“name” name=“name” size=“50”/> </p> <p> <label for=“email”>Email</label> <input type=“text” id=“email” name=“email” size=“50”/> </p> <p> <label for=“age”>Age</label> <input type=“text” id=“age” name=“age” size=“3”/> </p> 2. form.as_ul() 3. form.as_table()
  • 20. Rendering : Controlled Output {{ field.non_form_errors }} {% for field in form %} <div class=“fieldWrapper”> {{ field.errors }} {{ field.label_tag }}: {{ field }} </div> {% endfor %}
  • 21. Manually Created <form id=“contact” action=“” method=“get”> <p> <label for=“name”>Name</label> <input type=“text” id=“name” name=“name” size=“50”/> </p> <p> <label for=“email”>Email</label> <input type=“text” id=“email” name=“email” size=“50”/> </p> <p> <label for=“age”>Age</label> <input type=“text” id=“age” name=“age” size=“3”/> </p> <input type=“submit” name=“save”/> </form>
  • 22. Dynamically Created <form id=“contact” action=“” method=“get”> {{ form.as_p }} <input type=“submit” name=“save”/> </form> <form id=“contact” action=“” method=“get”> {{ field.non_form_errors }} {% for field in form %} <div class=“fieldWrapper”> {{ field.errors }} {{ field.label_tag }}: {{ field }} </div> {% endfor %} <input type=“submit” name=“save”/> </form>
  • 23. Model Forms  modelForms map a model to a form  validation includes model validators  Supports creating and editing instances  Key differences from forms  There will be a field for a primary key (id)  New save() method  New instance attribute
  • 24. Model Form Example app/models.py from django import models class Contact(models.Model): name = models.CharField(max_length=50) email = models.EmailField() age = models.IntegerField() app/forms.py from django import forms from grp.prj.app.models import Contact class ContactForm(forms.Modelform): class Meta: model = Contact
  • 25. Limiting Fields  Specify fields to expose, or fields to exclude app/forms.py from django import forms from grp.prj.app.models import Contact class ContactForm(forms.ModelForm): class Meta: model = Contact fields = ('name', 'email',) class ContactForm(forms.ModelForm): class Meta: model = Contact exclude = (‟age',)
  • 26. Instantiating Model Forms  Like before, we may have bound and unbound Model Forms Unbound mform = ContactForm() Bound mform = ContactForm(instance=Contact.objects.get(id=2) )
  • 27. Handling forms in views.py 1. Import your form(s) 2. Within your view function 1. Check for submission(GET or POST). 1. If submitted 1. bind the form to the data. 2. Validate. 1. If its valid, do stuff. 2. Otherwise let it remain unbound. 3. Pass form to the context 4. Render your page.
  • 28. views.py example from grp.prj.app.forms import ContactForm … def contactView(request): context = {} contactPDA = Dpdpcntc() … initials = { ‘age' : ’18' } #instantiate if request.GET.get(„submit‟): form = ContactForm(data = request.GET) if form.is_valid(): …. else: form = ContactForm(initial=initials) context[„form‟] = form return render_to_response(…) Import Form Check for submission Bind Form Form Validation Unbound Form Render HTML Form in Context
  • 29. views.py – other examples if request.GET.get(„submit‟): form = ContactForm(request.GET) if form.is_valid(): …. else: form = ContactForm(initial=initials) if request.GET: form = ContactForm(request.GET) if form.is_valid(): …. else: form = ContactForm(initial=initials) form = ContactForm(data=request.GET or None, initial=initials) if form.is_valid(): ….