Upload
phunghanh
View
231
Download
1
Embed Size (px)
Citation preview
Day 4: Introduc/on to Django
GSL South Africa 2015
Today’s Agenda
• The Big Picture • Django – A Web Applica/on Framework • Your First Django App • Models in Django • Today’s Assignment
2
GSL South Africa 2015
3
The Big Picture
GSL South Africa 2015
Once upon a /me…
4
OR
GSL South Africa 2015
Nowadays…
OR OR
GSL South Africa 2015
Implemen/ng the Backend
We need to implement the backend: • A web applica/on framework, like – MonoRail, CppCMS, Apache Click, Grails, Spring, Stripes, mul/ple, Catalyst, CakePHP, Drupal, Symfony, CherryPy, Django, web2py, Ruby on Rails, Compujure
• A host, like – Heroku, Google App Engine
• A RESTful API to communicate with frontend
6
GSL South Africa 2015
Web Applica/on Framework
7
• A framework (code libraries) to help you make web applica/ons or websites
• Supports you with – Handling HTTP requests – Templates for common HTML layouts – URL mapping – Database communica/on – Session management – Site security
GSL South Africa 2015
8
Django Web Applica/on Framework
Why Django?
GSL South Africa 2015
Why Django?
• Fast and easy development of web applica/ons – Modular and re-‐useable. Don’t Repeat Yourself (DRY) principle
– Built-‐in database management
• Ac/ve development and wide community support
• Supported by Google App Engine & Heroku
10
Django vs. PHP
GSL South Africa 2015
PHP SELECT Statement
GSL South Africa 2015
Django SELECT Statement
Django Architecture
GSL South Africa 2015
Model-‐View-‐Controller (MVC)
• A pacern for organizing code oeen seen in web app frameworks
• Main idea: 1. Separate storage and manipula/on of data (model)
from presenta/on of data (view) 2. Controller communicates between model and view
• Advantages – Develop and test model and view independently – Easier for others to understand (modularity)
16
GSL South Africa 2015
Model-‐View-‐Controller (MVC) (news site example)
17
Controller
View Model
• News stories and images in a database
• User comments
• Layout of stories on mobile phone or desktop browser
Send request for a story
Asks the model for the story and its user comments
Serves requested story
GSL South Africa 2015
Model-‐Template-‐View
In Django: Model-‐Template-‐View (similar to MVC pacern)
• Model – describes database informa/on
• Template – decides how to present informa/on
• View – manages what informa/on to output based on request
18
GSL South Africa 2015
Model-‐Template-‐View
19
View
Template Model
Send request for data
Asks the model for data
Provide Database Informa/on UI Layout Communicate
with Database
Handles Informa/on Exchange
Model
GSL South Africa 2015
20
Your First Django App
GSL South Africa 2015
Programming Interface
21
Terminal / Command Prompt Python IDLE or Favorite editor
GSL South Africa 2015
New Project
22
Project Structure: Whole project in one folder (mysite)
• MySite Python package • Applica/ons (polls) • (Database) • manage.py
• Use for interac/on with your project
• MySite Python Package • __init__.py • sejngs.py • urls.py • wsgi.py
GSL South Africa 2015
First Django Setup
Aeer a couple commands in terminal...
23
Models
24
GSL South Africa 2015
Models
25
View
Template Model
Send request for data
Asks the model for data
Provide Database Informa/on UI Layout Communicate
with Database
Handles Informa/on Exchange
GSL South Africa 2015
What is a Model
• Python class describing data in your applica/on – Subclass of models.Model
• Assigns acributes to each data field • Avoid direct work with the database – No need to handle database connec/ons, /meouts, etc. – Let Django do it for you!
– Provides Schema for database
26
GSL South Africa 2015
Django Model Syntax
27
Import statements
SubClass of models.Model Class
Define fields
Can define more func/ons
__unicode__ corresponds to python __str__
GSL South Africa 2015
Django Fields
We can define fields directly in our model class – No need to define manually in database
Example: create two fields in our Poll class
28
Define Type of Field • E.g. models.CharField
Define arguments of field • E.g. max_length=200
Django will automa/cally create fields in database
GSL South Africa 2015
Important Django Field Types • BooleanField
– Checkbox • CharField(max_length)
– Single-‐line textbox • DateField
– Javascript calendar • DateTimeField
– Javascript calendar, /me picker • DecimalField(max_digits, decimal_places)
– Decimal numbers • EmailField
– Charfield that validates email address
29
• FileField • File upload, stores path
in database • FloatField
• Floa/ng point numbers • IntegerField
Integer textbox • Posi/veIntegerField
• Integer textbos for posi/ve integers
• TextField • Mul/-‐line textbox
GSL South Africa 2015
Rela/onships
• Clearly, the power of rela/onal databases lies in rela/ng tables to each other. Django offers ways to define the three most common types of database rela/onships: many-‐to-‐one, many-‐to-‐many and one-‐to-‐one.
GSL South Africa 2015
Rela/onship Examples: Many-‐to-‐One
• A Car has a Manufacturer, but a Manufacturer can have many Cars
GSL South Africa 2015
Rela/onship Examples: Many-‐to-‐Many
• A Pizza can have mul/ple Toppings. Each type of Topping can be on mul/ple Pizzas
GSL South Africa 2015
Rela/onship Examples: Many-‐to-‐Many • Some/mes you need to associate data with the rela/onship between a Many-‐to-‐Many.
GSL South Africa 2015
Rela/onship Examples: One-‐to-‐One
GSL South Africa 2015
Rules of Django Models • When you update a model, ALWAYS RUN python manage.py migrate
• All classes extend models.Model • Models only live in Apps • Django doesn't save objects un/l you call save() method
>>>a1 = Album(...) # a1 is not saved to the database yet! >>>a1.save() # Now it is.
35
GSL South Africa 2015
Today’s Assignment
Get started with Django! – If you haven’t already, install Django 1.8 – Create your first Django app:Wri/ng your first Django App – Part 1
Wri/ng your first Django App – Part 2 – Come up with preliminary data model for you project
Helpful Documenta/on: – Models sec/on of Django docs
36