Django Forms Basics and Cool Things

Looking into Forms basics, Dynamically creating forms, Formsets Creating Forms

What are Forms?

  • If you want a website that can take user input you should use html forms
  • But i'm sure you guys know that:)

Why use Django Forms?

  • Easy to create html forms and insert them in a template.
  • Help Developers from creating vulnerabilities.
  • Security, Security, Security!

Creating Basic Forms

Creating your own forms with basic fields.

Basic Form with a variety of fields

arugments for form fields

  • label
  • widget
  • blank,null
  • required
  • initial

Using the basic form in a view

Forms in templates

  • rendering the basic form
  • using the .as_ul .as_p methods
  • making your own forms

Files in Forms

  • Uploading files you need to enctype="multipart/form-data" to the form
  • you need to use request.FILES (rather than request.POST to access the files
  • you also need to specify where your files will be saved with your media_root

ModelForms

Are awesome and will make your life better and should be used a lot!

Why are ModelForms so cool?

  • Make you do less work
  • Forms html automatically created!
  • saves the validated and cleaned data.

What is a model form?

  • Forms basically a form based on your model.

ModelForm

Model

ModelForm in a view

This is 90% of forms.

Knowing basic forms and model forms will get you really far in django.

Moving to more cool things with forms

Formsets and

Dynamically building forms

 

Formsets

A short introduction as well as trying to make them not as scary.

What are formsets?

  • A formset allows you to have multiple forms on one page.
  • say for example emails for friends that you'd like to send to.

How to use a formset

Using formsets are easy

  • Use your already existing form and create a formset with the formset_factory method ( where you use the form)
  • Or if you're using a model form you can use the modelformset_factory (where you use the model)

How to show a formset in a template

  • In the form include the "formset.management_form" to the from.
  • Loop through the formset to print out the forms.

How to add more forms dynamically? Part 1

  • To the right you see the solution I've come up with
  • along with rendering the formset, render an empty form to append the formset

How to add more forms dynamically? Part 2

  • This solution uses underscore.js and jquery
  • takes the empty form changes the id.
  • Changes the form management. of the formset

Building Forms Dynamically

Creating Forms that can be based off of data in a model.

Create Models

First thing we have to do is create the models. Here we create two models: Survey and Question.

Custom Form

Here our form is created the questions that saved based on the survey key.

Our field types are a one to one mapping of the field type choices in our models.

View for our dynamic form

Here our form is created the questions that saved based on the survey key.

Our field types are a one to one mapping of the field type choices in our models.

Questions?

Thank you so much!

  • linkedin(send me a message): Daniel Mouris 
  • Twitter(I don't use it that much): @dgmouris 
  • Have fun coding! 
Made with Slides.com