36
Day 4: Introduc/on to Django

Day 4: Introduc/on to Django | Django Model Syntax

Embed Size (px)

Citation preview

Page 1: Day 4: Introduc/on to Django | Django Model Syntax

Day  4:  Introduc/on  to  Django  

Page 2: Day 4: Introduc/on to Django | Django Model Syntax

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  

Page 3: Day 4: Introduc/on to Django | Django Model Syntax

GSL  South  Africa  2015    

3  

The  Big  Picture  

Page 4: Day 4: Introduc/on to Django | Django Model Syntax

GSL  South  Africa  2015    

Once  upon  a  /me…  

4  

OR  

Page 5: Day 4: Introduc/on to Django | Django Model Syntax

GSL  South  Africa  2015    

Nowadays…  

OR   OR  

Page 6: Day 4: Introduc/on to Django | Django Model Syntax

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  

Page 7: Day 4: Introduc/on to Django | Django Model Syntax

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  

Page 8: Day 4: Introduc/on to Django | Django Model Syntax

GSL  South  Africa  2015    

8  

Django    Web  Applica/on  Framework  

Page 9: Day 4: Introduc/on to Django | Django Model Syntax

Why  Django?  

Page 10: Day 4: Introduc/on to Django | Django Model Syntax

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  

Page 11: Day 4: Introduc/on to Django | Django Model Syntax
Page 12: Day 4: Introduc/on to Django | Django Model Syntax

Django  vs.  PHP  

Page 13: Day 4: Introduc/on to Django | Django Model Syntax

GSL  South  Africa  2015    

PHP  SELECT  Statement  

Page 14: Day 4: Introduc/on to Django | Django Model Syntax

GSL  South  Africa  2015    

Django  SELECT  Statement  

Page 15: Day 4: Introduc/on to Django | Django Model Syntax

Django  Architecture  

Page 16: Day 4: Introduc/on to Django | Django Model Syntax

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  

Page 17: Day 4: Introduc/on to Django | Django Model Syntax

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  

Page 18: Day 4: Introduc/on to Django | Django Model Syntax

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  

Page 19: Day 4: Introduc/on to Django | Django Model Syntax

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  

Page 20: Day 4: Introduc/on to Django | Django Model Syntax

GSL  South  Africa  2015    

20  

Your  First  Django  App  

Page 21: Day 4: Introduc/on to Django | Django Model Syntax

GSL  South  Africa  2015    

Programming  Interface  

21  

Terminal  /  Command  Prompt   Python  IDLE  or  Favorite  editor  

Page 22: Day 4: Introduc/on to Django | Django Model Syntax

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  

 

Page 23: Day 4: Introduc/on to Django | Django Model Syntax

GSL  South  Africa  2015    

First  Django  Setup  

Aeer  a  couple  commands  in  terminal...  

23  

Page 24: Day 4: Introduc/on to Django | Django Model Syntax

Models  

24  

Page 25: Day 4: Introduc/on to Django | Django Model Syntax

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  

Page 26: Day 4: Introduc/on to Django | Django Model Syntax

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  

Page 27: Day 4: Introduc/on to Django | Django Model Syntax

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__    

Page 28: Day 4: Introduc/on to Django | Django Model Syntax

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    

Page 29: Day 4: Introduc/on to Django | Django Model Syntax

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  

 

Page 30: Day 4: Introduc/on to Django | Django Model Syntax

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.  

Page 31: Day 4: Introduc/on to Django | Django Model Syntax

GSL  South  Africa  2015    

Rela/onship  Examples:  Many-­‐to-­‐One  

•  A  Car  has  a  Manufacturer,  but  a  Manufacturer  can  have  many  Cars  

Page 32: Day 4: Introduc/on to Django | Django Model Syntax

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  

Page 33: Day 4: Introduc/on to Django | Django Model Syntax

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.  

Page 34: Day 4: Introduc/on to Django | Django Model Syntax

GSL  South  Africa  2015    

Rela/onship  Examples:  One-­‐to-­‐One  

Page 35: Day 4: Introduc/on to Django | Django Model Syntax

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  

Page 36: Day 4: Introduc/on to Django | Django Model Syntax

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