Transcript
Page 1: django Forms in a Web API World

django Forms in an API World

http://www.northwesternflipside.com/2011/01/10/2011-where-are-the-flying-cars/

XHR

Page 2: django Forms in a Web API World

Lets talk about me

Tareque HossainLead Software Engineer

Page 3: django Forms in a Web API World

Lets talk about forms

django Forms

And how we can use them in this day & age of APIs!

Page 4: django Forms in a Web API World

What can you expect…

• What’s wrong with forms as it is• How we use forms• Issues using form in an API

world• Approaches for tackling the

issues• The solution

Page 5: django Forms in a Web API World

The good old days..

• Write up some HTML• Throw some fancy template tags in

there{{ my_awesome_form.as_p }}

• WIN

Page 6: django Forms in a Web API World

Fast forward a few years..

You really dig APIs

Page 7: django Forms in a Web API World

Your setup looks like this:

I give you APINo $#!7.

Me likey!

Page 8: django Forms in a Web API World

Nuevo mundo..

• Django forms live on API server–Validates/ saves API requests–Doesn’t get rendered via template

• You’ve been writing forms in the frontend–Hardcoded HTML–Trying to match up data that API

expects

Page 9: django Forms in a Web API World

API Clients

• Your website no longer lives on the same application space as the API

• Common API clients–A JavaScript MVC powered website–An android app–An iOS app

Page 10: django Forms in a Web API World

Forms on API Clients

Page 11: django Forms in a Web API World

The Issue

• You can serve most platforms with an HTML app–Write form in HTML on your

webapp

• If you write native application for mobile–You recreate forms using the

interfaces available

Page 12: django Forms in a Web API World

The Issue

• These interfaces you write–Don’t have any idea about the django

forms residing on the API server–Only know what data to collect when

you explicitly code them on each device

• There’s a disconnect

Page 13: django Forms in a Web API World

Houston we have a problem..

http://epicdemotivational.com/tag/beer/page/2/

Page 14: django Forms in a Web API World

Lets take a step back

What is a form?

Page 15: django Forms in a Web API World

Lets take a step back

a printed or typed document with blank spaces for insertion of required or requested information

ˈform (noun)

Entry #4 at http://www.merriam-webster.com/dictionary/form

Page 16: django Forms in a Web API World

In the world of HTML

Part of an HTML document with input interfaces for inserting required or requested information

Page 17: django Forms in a Web API World

In the world of web apps

• A form is the interface we provide the application user to collect information

• It’s essential to any application where we collect data

Page 18: django Forms in a Web API World

In the world of django

Page 19: django Forms in a Web API World

django Forms

• A construct that:–Binds data (request.POST)–Validates data (clean)–Processes data (save)–Renders interface (as_p)

Page 20: django Forms in a Web API World

django Forms

• ModelForm–Turns your model into a form–Easiest way to get an interface for

your data

• Widgets–Define specific properties for interface

element–Separates presentation from data

types

Page 21: django Forms in a Web API World

Why not just render via template?

You can’t if:–You only use django to power your

API and the consumers are arbitrary–You run several django API servers

each dealing with different data space

Page 22: django Forms in a Web API World

Think about this architecture

Content API

Profile API

Analytics API

User AppAdmin App

Page 23: django Forms in a Web API World

Your services are distributed

• Web applications we design are increasingly becoming:–Separated between presentation

and data layer via API–Dependent on multiple API

endpoints–Rich and complex in interface

Page 24: django Forms in a Web API World

Your services are distributed

• Your site content is retrieved using the Content API–You collect user feedback on

content using forms–You provide admin interface to

manage content using forms

Page 25: django Forms in a Web API World

Your services are distributed

• Information for users are stored and retrieved using Profile API–You allow log in, creation and

update of profiles using forms–You provide admin interface to

manage profiles using forms

Page 26: django Forms in a Web API World

Your services are distributed

• Site performance and user traffic is recorded to Analytics API–You provide admin interface to

access and create custom reports using forms

Page 27: django Forms in a Web API World

Think again.

Content API

Profile API

Analytics API

User AppAdmin App

Page 28: django Forms in a Web API World

The Issue (contd.)

• At WiserTogether we love APIs & have a similar distributed setup

• We’ve been hardcoding forms in the frontend, collecting data and sending to API

Page 29: django Forms in a Web API World

The Issue (contd.)

• Whenever a data point in the backend changed, we had to update the form

• We have multiple clients who require different set of fields present on registration forms–Again, hardcoding in frontend

Page 30: django Forms in a Web API World

It was a mess$#^*!

Page 31: django Forms in a Web API World

What to do..

• django forms is used to validate and process our API data

• We wanted django forms to determine our frontend interface–But it was completely agnostic

about backend forms!

Page 32: django Forms in a Web API World

What to do..

• Deliver form definition over API• Render interface in the frontend

from the retrieved definition–No more hardcoding–Forms in the user facing

application changes as soon as backend form changes

Page 33: django Forms in a Web API World

What to do..

• Adjust form in whatever way necessary–Add/ remove fields from

registration form–Frontend renders form exactly the

way you want–No code change necessary in

frontend

Page 34: django Forms in a Web API World

What to do..

• Contain form definition in one spot• Allow a single central point to

control interface on all applications• Allow different API consumers to

retrieve form definition–And render interface appropriate for

the platform or device

Page 35: django Forms in a Web API World

3 step solution

Page 36: django Forms in a Web API World

Step 1

Serialize form with all information necessary for reproduction at frontend

Page 37: django Forms in a Web API World

Step 2

• Devise methods to handle the following over API:–Deliver form definition–Receive form data–Validate form and deliver errors– If valid save the form

Page 38: django Forms in a Web API World

Step 3

• Handle forms in the frontend using API data–Render form–Submit data– If not valid, then display errors– If valid, then display success

message, reload page or redirect as necessary

Page 39: django Forms in a Web API World

Step 1

Serialize form with all information necessary for reproduction at frontend

Page 40: django Forms in a Web API World

django Remote Forms

• Extracts all information from a given django form or model form instance

• Goes through each field & widget to extract all necessary information

• Presents the information as dictionary for further manipulation & easy serialization into JSON

Page 41: django Forms in a Web API World

As easy as π

Page 42: django Forms in a Web API World

A JSON form

Page 43: django Forms in a Web API World

Step 2

• Devise methods to handle the following over API:–Deliver form definition–Receive form data–Validate form and deliver errors– If valid save the form

Page 44: django Forms in a Web API World
Page 45: django Forms in a Web API World

Points to Ponder

• Handle CSRF yourself of using X-CSRFToken–django CSRF middleware is not

JSON friendly

• Encapsulate form processing in save method, similar to Model Form

Page 46: django Forms in a Web API World

Step 3

• Handle forms in the frontend using API data–Render form–Submit data– If not valid, then display errors– If valid, then display success

message, reload page or redirect as necessary

Page 47: django Forms in a Web API World

HTML/JS/CSS Implementation

• We created a set of rendering and data handling tools for the frontend using:

• In future, we’ll be working towards iOS implementations as well

Page 48: django Forms in a Web API World

Backbone Form Handler

• Renders forms based on definition received over API

• Uses Handlebars template for rendering:– Core form structure (form tag, fields

container, global errors)–Field & widget structure (help text,

errors)

• Error rendering

Page 49: django Forms in a Web API World

Backbone Form Handler• Allows instant validation–Similar to autocomplete–Field can be validated as soon as you

move to next one

• Allows preloading of data• Disallow editing of fields– Including selects, radio and checkboxes

• Provide submit buttons (if not supplied)

Page 50: django Forms in a Web API World

Handlebars Templates

Handlebars Widgets

Page 51: django Forms in a Web API World

Sample Backbone View

Instantiate form model

Instantiate form view

Initiate rendering by fetching the form definition

Page 52: django Forms in a Web API World
Page 53: django Forms in a Web API World

django Remote Admin

• A reviewer expressed interest–Use remote forms to expose django

admin interface over API

• So I implemented a set of API endpoints– Exposes django admin

app/model/instance data– Exposes admin forms

• And wrote a backbone app implementing django admin

Page 54: django Forms in a Web API World

Goals of django Remote Admin

• Allow administration of django projects over API

• No more ties to the same old interface!

• Use awesome Handlebars snippets of your own to spice up the interface

Page 55: django Forms in a Web API World

How does it work?

• Cycle through admin site registry– Extract app/model info and expose

over API

• Create ModelForm from the model– Expose over API using django remote

forms

• The backbone app calls the API– Allows browsing apps/ models– Allows creating/editing model

instances

Page 56: django Forms in a Web API World

Further Work

• django Remote Forms– Implement file/ image uploading

over API

• django Remote Admin–Load form/widget customizations

from Admin classes– Implement pagination for foreign

key loader

Page 57: django Forms in a Web API World

Demo

• Ask me about WiserTogether– github.com/WiserTogether/django-remote-

forms– github.com/tarequeh/django-remote-admin

• Follow my tweets @tarequeh• Shout out to Carlo Costino• ind this presentation–slideshare.net/tarequeh

Page 58: django Forms in a Web API World

Q/A

• Ask me about WiserTogether– github.com/WiserTogether/django-remote-

forms– github.com/tarequeh/django-remote-admin

• Follow my tweets @tarequeh• Shout out to Carlo Costino• ind this presentation–slideshare.net/tarequeh


Top Related