This talk looks at the core concepts of Django Forms and explores a few third party packages. Talk was given to the Django Boston Meetup on 27JUN2013 and Django NYC on 24OCT2013.
only art form that allows you to create interactive art. You can create projects that other people can play with, and you can talk to them indirectly. No other art form is quite this interactive. Movies flow to the audience in one direction. Paintings do not move. Code goes both ways.” -- Zed Shaw, Learn Python the Hard Way; “Advice From An Old Programmer” 5 Friday, November 1, 13
4 class ContactForm(forms.Form): 5 subject = forms.CharField(max_length=100) 6 message = forms.CharField() 7 sender = forms.EmailField() 8 cc_myself = forms.BooleanField(required=False) 1 #### views.py #### 2 from django.shortcuts import render 3 from django.http import HttpResponseRedirect 4 5 def contact(request): 6 if request.method == 'POST': # If the form has been submitted... 7 form = ContactForm(request.POST) # A form bound to the POST data 8 if form.is_valid(): # All validation rules pass 9 # Process the data in form.cleaned_data 10 # ... 11 return HttpResponseRedirect('/thanks/') # Redirect after POST 12 else: 13 form = ContactForm() # An unbound form 14 15 return render(request, 'contact.html', { 16 'form': form, 17 }) 9 BASIC FORM 2.
Render Model fields as HTML • Select validators based off Model field definitions • Don’t have to display/change all available fields • Save dictionaries to SQL tables •[Model]Forms
for Django 1.5 (Kindle Locations 1949-1950). Two Scoops Press. • 95% of Django projects should use ModelForms. • 91% of all Django projects use ModelForms. • 80% of ModelForms require trivial logic. • 20% of ModelForms require complicated logic. – pydanny made-up statistics 15 MODELFORMS IN REAL LIFE Friday, November 1, 13
from django.http import HttpResponseRedirect 4 5 def contact(request): 6 if request.method == 'POST': # If the form has been submitted... 7 form = ArticleForm(request.POST) 8 if form.is_valid(): 9 # Process the data in form.cleaned_data 10 form.save() 11 12 return HttpResponseRedirect('/success/') # Redirect after POST 13 else: 14 form = ArticleForm() # Still an unbound form 15 16 return render(request, 'article.html', { 17 'form': form, 18 }) 17 BASIC MODELFORM 1.
an HTML form widget, e.g. <input type="text"> or <textarea>. This handles rendering of the widget as HTML.” Field “A class that is responsible for doing validation, e.g. an EmailField that makes sure its data is a valid email address.” Form “A collection of fields that knows how to validate itself and display itself as HTML.” Form Media “The CSS and JavaScript resources that are required to render a form.” 21 Friday, November 1, 13
False, else TextInput. Empty value: None Normalizes to: A Python decimal. Validates that the given value is a decimal. Leading and trailing whitespace is ignored. Error message keys: required, invalid, max_value, min_value, max_digits, max_decimal_places, max_whole_digits 24 FIELDS: DETAILED EXAMPLE What
RangeInput: <input type="range"> for sliders instead of spinboxes for numbers PhoneNumberInput: <input type="tel"> For phone numbers SearchInput: <input type="search">.” 52 Friday, November 1, 13
for space You, for showing up The Django team, for Django Contributors for their contributions to third-party packages @pydanny and @audreyr for putting out Two Scoops of Django (buy it!), and a bunch of helpful form blog posts, and crispy-forms 61 Friday, November 1, 13