SlideShare ist ein Scribd-Unternehmen logo
1 von 34
Downloaden Sie, um offline zu lesen
Django & App Engine	

Developing and Deploying to the Cloud	

            Yared Ayalew	

            @yaredayalew
Agenda	


•  Django   101	


•  Using   Django	


•  Django   and App Engine
Not this Django!
What is Django?	


“The web framework for perfectionists with deadlines”	


   Django makes it easier to build better web apps
          more quickly and with less code	


             www.djangoproject.com
What s it for?	


•  Building   dynamic websites	


•  A   high level web framework	


•  Abstracts   common problems	


•  Shortcuts   for fast development
The Framework	

•  Elegant       URL Design	


•  Object/Relational              Mapper (ORM)	


•  Powerful         Templating System	


•  Automatic          Admin Interface	


•  i18n	


•    caching, syndication, middleware, email, sql, modules, authentication, sessions, comments, sitemaps,
     gis ...
Architecture	

•     MTV 	


•  Models       describe your data	


•  Views       control what a user sees and
     does	


•  Templates       are what users see
Using Django
Requirements	


•  Python   2.3+	


•  PostgreSQL/MySQL/SQLite	


•  Apache   + mod_python/mod_wsgi/FastCGI
Pip	


•  A   tool for installing and managing Python packages	


•  PIP   (Pip installs Python)	


$	
  wget	
  http://pypi.python.org/packages/source/p/pip/pip-­‐1.2.1.tar.gz	
  
	
  
$	
  tar	
  xzf	
  pip-­‐1.2.1.tar.gz	
  
	
  
$	
  cd	
  pip-­‐1.2.1	
  
$	
  python	
  setup.py	
  install	
  
	
  
virtualenv	


•  A   self-contained virtual environment for Python development	


  •  Does   not touch your Python installation	


  •  Keep   track of needed modules with a requirements file	


  •  Allows   to test several package versions	


$	
  pip	
  install	
  virtualenv	
  
Creating a Virtualenv	

Create	
  the	
  virtual	
  environment	
  
$	
  virtualenv	
  myenvironment	
  
	
  
Activate	
  the	
  virtual	
  environment	
  
$	
  cd	
  myenvironment	
  
$	
  source	
  bin/activate	
  
(myenvironment)$	
  
	
  
	
  
Get Django	


     Download it and install from 	


http://www.djangoproject.com/download	

                 OR	

    $	
  pip	
  install	
  django	
  
Development
Create A Project	

$	
  django-­‐admin.py	
  startproject	
  myproject	
  
	
  
myproject/	
  
	
  manage.py	
  
	
  myproject/	
  
	
  	
  	
  __init__.py	
  
	
  	
  	
  settings.py	
  
	
  	
  	
  urls.py	
  
	
  	
  	
  wsgi.py	
  
	
  
	
  
Running a Project	

$	
  python	
  manage.py	
  runserver	
  
	
  
 Browse to http://localhost:8000
Projects and Apps	

“A project is a collection of settings for an instance of Django,
 including database configuration, Django-specific options and
                 application-specific settings.”	



"A bundle of Django code, including models and views, that lives
together in a single Python package and represents a full Django
                          application."
Creating Applications	

$	
  python	
  manage.py	
  startapp	
  myapp	
  
	
  
myapp/	
  
	
  	
  __init__.py	
  
	
  	
  models.py	
  
	
  	
  tests.py	
  
	
  	
  views.py	
  
	
  
	
  
Django Tools	

•  Django   Shell – manipulate your models	


    	

python	
  manage.py	
  shell	
  

•  Django   Server – development & debugging environment	


    	

python	
  manage.py	
  server	
  

•  SyncDb   – build your database from models	


    	

python	
  manage.py	
  syncdb	
  
Django Building blocks
Project Configuration	

•  Easy   configuration in file settings.py	
  

•  Allows   you to configure:	


  •  Database      connection	


  •  Installed   apps	


  •  Template       path	


  •  Logging     etc.
Models	


•  Pythonclasses that represent objects in the database and is a
 subclass of django.db.models.Model	
  

•  Each   attribute of the model represents a database field	

class	
  Location(models.Model):	
  	
  	
  	
  	
  
	
  	
  	
  	
  	
  location_description	
  =	
  models.TextField()	
  
	
  	
  	
  	
  	
  city	
  =	
  models.CharField("City",max_length=200,null=True)	
  
	
  	
  	
  	
  	
  common_name	
  =	
  models.CharField(max_length=200)	
  
	
  	
  	
  	
  	
  lat	
  =	
  models.FloatField("Latitude",blank=True,null=True)	
  
	
  	
  	
  	
  	
  lon	
  =	
  models.FloatField("Longitude",blank=True,null=True)	
  
	
  
	
  
Views	

•  APython function that takes a web request and returns a web
  response.	


•  Response      can be html, redirection, 404, image, binary stream,
  xml etc.	

from	
  myapp.models	
  import	
  Location	
  
	
  
def	
  location_by_city(request,city):	
  
	
  	
  	
  	
  places	
  =	
  Location.objects.filter(city=city)	
  
	
  	
  	
  	
  return	
  render_to_response(‘locations/result.html’,{‘places’:places})	
  	
  	
  
	
  
	
  
Templates	

•  Atext file that can generate any text based format (html, xml, cvs
  etc)	


•  Designer    friendly	


•  Tags,   variables and filters	


•  {%	
  if	
  %}	
  {%	
  else	
  %}	
  {%	
  endif	
  %}	
  

•  {%	
  for	
  item	
  in	
  list	
  %}	
  	
  {%	
  endfor	
  %}	
  

•  {%	
  ifequal	
  %}	
  {%	
  endifequal	
  %}	
  

•  {{	
  name|lower	
  }}	
  
Philosophies and Limitations	

•  Business   logic should be separated from presentation logic	


•  Syntax   should be decoupled from HTML/XML	


•  Designers   are assumed to be comfortable with HTML code	


•  Designers   are assumed not to be Python programmers	


•  The   goal is not to invent a programming language
base.html	
  
                                            Templates	

<html>	
  
	
  	
  <head>	
  
	
  	
  	
  	
  <title>{%	
  block	
  title	
  %}{%	
  endblock	
  %}</title>	
  
	
  	
  </head>	
  
	
  	
  <body>	
  
	
  	
  	
  	
  	
  {%	
  block	
  content	
  %}	
  {%	
  endblock	
  %}	
  
	
  	
  </body>	
  
</html>	
  
	
  
result.html	
  
{%	
  extends	
  “base.html”	
  %}	
  
{%	
  block	
  title	
  %}	
  Search	
  Result	
  {%	
  endblock%}	
  
{%	
  block	
  content	
  %}	
  
	
  	
  	
  	
  	
  <h2>The	
  following	
  locations	
  are	
  found:</h2>	
  
	
  	
  	
  	
  	
  {%	
  for	
  place	
  in	
  places	
  %}	
  
	
  	
  	
  	
  	
  	
  	
  	
  <h4>{{	
  place.common_name	
  }}</h4>	
  
	
  	
  	
  	
  	
  	
  	
  	
  <p>	
  {{	
  place.location_description	
  }}	
  </p>	
  
	
  	
  	
  	
  	
  	
  	
  	
  <img	
  src=’{%	
  url	
  place.get_map	
  %}’/>	
  
	
  	
  	
  	
  	
  {%	
  endfor	
  %}	
  
{%	
  endblock	
  %}	
  
	
  
URLs	

 •  A   mapping between a regex url pattern and view functions	


 •  Part   of the overall application design	


 •  defined      inside urls.py	
  

from	
  django.conf.urls	
  import	
  patterns,	
  include,	
  url	
  
	
  
urlpatterns	
  =	
  patterns('',	
  	
  	
  	
  	
  
      	
  url(r'^$',	
  'myapp.index'),	
  
      	
  url(r'^places/(?P<city>w+)/)$',	
  'myapp.location_by_city'),	
  	
  	
  	
  	
  	
  
      	
  url(r'^place/(?P<location_id>d+)/$','myapp.view_place'),)	
  
	
  
Forms	

 •  Classes          that represent html forms	


 •  Allow         data input, validation, error message, label etc.	

class	
  CreateLocation(forms.Form):	
  
	
  	
  	
  description	
  =	
  forms.TextField()	
  
	
  	
  	
  city	
  =	
  forms.CharField(label=”City”,max_length=200)	
  
	
  	
  	
  common_name	
  =	
  forms.CharField(max_length=200,widget=	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  forms.TextInput(attrs={‘class’:‘medium_text’}))	
  
	
  	
  	
  lat	
  =	
  forms.FloatField()	
  
	
  	
  	
  lon	
  =	
  forms.FloatField()	
  
	
  
from	
  django.forms	
  import	
  models	
  
class	
  LocationForm(model.ModelForm):	
  
	
  	
  	
  	
  	
  	
  class	
  Meta:	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  model=Location	
  
	
  
Forms	

views.py	
  
	
  
def	
  new_location(request):	
  
	
  	
  	
  	
  if	
  request.method==‘POST’:	
  
	
  	
  	
  	
  	
  	
  	
  form	
  =	
  CreateLocation(request.POST)	
  
	
  	
  	
  	
  	
  	
  	
  if	
  form.is_valid():	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  #	
  Create	
  a	
  new	
  location	
  object	
  and	
  save	
  it	
  to	
  the	
  database	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  return	
  HttpResponseRedirect(‘/index/’)	
  
	
  	
  	
  	
  	
  	
  	
  else:	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  form	
  =	
  CreateLocation()	
  
	
  	
  	
  	
  	
  	
  	
  return	
  render_to_response(‘new.html’,{‘form’:form})	
  
	
  

new.html	
  
<form	
  action	
  =	
  “/new/”	
  method=“POST”>	
  
	
  	
  	
  	
  	
  {%	
  csrf_token	
  %}	
  
	
  	
  	
  	
  	
  {{	
  form.as_p	
  }}	
  
	
  	
  	
  	
  	
  <input	
  type=“submit”	
  value=“Save”/>	
  
</form>	
  
Automatic Admin	

•  Gives     you admin interface to manage your models	


•  Built   into the framework	


•  To    enable admin interface in urls.py uncomment	

from	
  django.contrib	
  import	
  admin	
  
admin.autodiscover()	
  
	
  
url(r'^admin/',	
  include(admin.site.urls))	
  
	
  
#	
  inside	
  your	
  app	
  add	
  a	
  file	
  called	
  admin.py	
  
from	
  django.contrib	
  import	
  admin	
  
from	
  models	
  import	
  Location	
  
	
  
admin.site.register(Location)	
  
Django + App Engine
Django App Engine	

•  Supports   Non-relational models (NOSQL)	


•  No   support for Django’s ImageField and ManyToManyField	


•  Aggregates	


•  Transactions	


•  Many-to-many      relations	


•  QuerySet.select_related()        – a queryset that follows foreign-
 key relationship.
Google Cloud SQL	

•  Fully
     managed relational database based on MySQL that lives in
  Google’s cloud.	


•  Currently   in beta and requires to enable billing	


•  Restrictions:	


  •  100   GB size limit per instance	


  •  No    support for user defined functions	


  •  MySQL     replication is not supported	


•  More    detail at https://developers.google.com/cloud-sql/

Weitere ähnliche Inhalte

Was ist angesagt?

Testing untestable Code - PFCongres 2010
Testing untestable Code - PFCongres 2010Testing untestable Code - PFCongres 2010
Testing untestable Code - PFCongres 2010Stephan Hochdörfer
 
Phing for power users - dpc_uncon13
Phing for power users - dpc_uncon13Phing for power users - dpc_uncon13
Phing for power users - dpc_uncon13Stephan Hochdörfer
 
Jquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript BasicsJquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript BasicsEPAM Systems
 
Ruby on Rails ステップアップ講座 - 大場寧子
Ruby on Rails ステップアップ講座 - 大場寧子Ruby on Rails ステップアップ講座 - 大場寧子
Ruby on Rails ステップアップ講座 - 大場寧子Yasuko Ohba
 
Sightly - AEM6 UI Development using JS and JAVA
Sightly - AEM6 UI Development using JS and JAVASightly - AEM6 UI Development using JS and JAVA
Sightly - AEM6 UI Development using JS and JAVAYash Mody
 
django_introduction20141030
django_introduction20141030django_introduction20141030
django_introduction20141030Kevin Wu
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQueryAlek Davis
 
jQuery introduction
jQuery introductionjQuery introduction
jQuery introductionTomi Juhola
 
Active Record Form Helpers, Season 1
Active Record Form Helpers, Season 1Active Record Form Helpers, Season 1
Active Record Form Helpers, Season 1RORLAB
 
jQuery Rescue Adventure
jQuery Rescue AdventurejQuery Rescue Adventure
jQuery Rescue AdventureAllegient
 

Was ist angesagt? (19)

Testing untestable Code - PFCongres 2010
Testing untestable Code - PFCongres 2010Testing untestable Code - PFCongres 2010
Testing untestable Code - PFCongres 2010
 
Discovering Django - zekeLabs
Discovering Django - zekeLabsDiscovering Django - zekeLabs
Discovering Django - zekeLabs
 
Phing for power users - dpc_uncon13
Phing for power users - dpc_uncon13Phing for power users - dpc_uncon13
Phing for power users - dpc_uncon13
 
Jquery 4
Jquery 4Jquery 4
Jquery 4
 
Jquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript BasicsJquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript Basics
 
Ruby on Rails ステップアップ講座 - 大場寧子
Ruby on Rails ステップアップ講座 - 大場寧子Ruby on Rails ステップアップ講座 - 大場寧子
Ruby on Rails ステップアップ講座 - 大場寧子
 
JS Essence
JS EssenceJS Essence
JS Essence
 
Real World MVC
Real World MVCReal World MVC
Real World MVC
 
Sightly - AEM6 UI Development using JS and JAVA
Sightly - AEM6 UI Development using JS and JAVASightly - AEM6 UI Development using JS and JAVA
Sightly - AEM6 UI Development using JS and JAVA
 
Crafting [Better] API Clients
Crafting [Better] API ClientsCrafting [Better] API Clients
Crafting [Better] API Clients
 
Wt unit 4 server side technology-2
Wt unit 4 server side technology-2Wt unit 4 server side technology-2
Wt unit 4 server side technology-2
 
django_introduction20141030
django_introduction20141030django_introduction20141030
django_introduction20141030
 
Jsp
JspJsp
Jsp
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
Presentation
PresentationPresentation
Presentation
 
jQuery introduction
jQuery introductionjQuery introduction
jQuery introduction
 
Active Record Form Helpers, Season 1
Active Record Form Helpers, Season 1Active Record Form Helpers, Season 1
Active Record Form Helpers, Season 1
 
jQuery Rescue Adventure
jQuery Rescue AdventurejQuery Rescue Adventure
jQuery Rescue Adventure
 
Django a whirlwind tour
Django   a whirlwind tourDjango   a whirlwind tour
Django a whirlwind tour
 

Ähnlich wie GDG Addis - An Introduction to Django and App Engine

Django Overview
Django OverviewDjango Overview
Django OverviewBrian Tol
 
Web development with django - Basics Presentation
Web development with django - Basics PresentationWeb development with django - Basics Presentation
Web development with django - Basics PresentationShrinath Shenoy
 
Introduction Django
Introduction DjangoIntroduction Django
Introduction DjangoWade Austin
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to DjangoJoaquim Rocha
 
An Introduction to Tornado
An Introduction to TornadoAn Introduction to Tornado
An Introduction to TornadoGavin Roy
 
Mezzanine簡介 (at) Taichung.py
Mezzanine簡介 (at) Taichung.pyMezzanine簡介 (at) Taichung.py
Mezzanine簡介 (at) Taichung.pyMax Lai
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...Fabio Franzini
 
Python & Django TTT
Python & Django TTTPython & Django TTT
Python & Django TTTkevinvw
 
A Basic Django Introduction
A Basic Django IntroductionA Basic Django Introduction
A Basic Django IntroductionGanga Ram
 
Gae Meets Django
Gae Meets DjangoGae Meets Django
Gae Meets Djangofool2nd
 
بررسی چارچوب جنگو
بررسی چارچوب جنگوبررسی چارچوب جنگو
بررسی چارچوب جنگوrailsbootcamp
 
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)Doris Chen
 
Staying Sane with Drupal NEPHP
Staying Sane with Drupal NEPHPStaying Sane with Drupal NEPHP
Staying Sane with Drupal NEPHPOscar Merida
 
Django Rest Framework and React and Redux, Oh My!
Django Rest Framework and React and Redux, Oh My!Django Rest Framework and React and Redux, Oh My!
Django Rest Framework and React and Redux, Oh My!Eric Palakovich Carr
 
WRStmlDSQUmUrZpQ0tFJ4Q_a36bc57fe1a24dd8bc5ba549736e406f_C2-Week2.pptx
WRStmlDSQUmUrZpQ0tFJ4Q_a36bc57fe1a24dd8bc5ba549736e406f_C2-Week2.pptxWRStmlDSQUmUrZpQ0tFJ4Q_a36bc57fe1a24dd8bc5ba549736e406f_C2-Week2.pptx
WRStmlDSQUmUrZpQ0tFJ4Q_a36bc57fe1a24dd8bc5ba549736e406f_C2-Week2.pptxsalemsg
 
Jumpstart Django
Jumpstart DjangoJumpstart Django
Jumpstart Djangoryates
 

Ähnlich wie GDG Addis - An Introduction to Django and App Engine (20)

Django Overview
Django OverviewDjango Overview
Django Overview
 
Web development with django - Basics Presentation
Web development with django - Basics PresentationWeb development with django - Basics Presentation
Web development with django - Basics Presentation
 
Introduction Django
Introduction DjangoIntroduction Django
Introduction Django
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
 
An Introduction to Tornado
An Introduction to TornadoAn Introduction to Tornado
An Introduction to Tornado
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
 
Mezzanine簡介 (at) Taichung.py
Mezzanine簡介 (at) Taichung.pyMezzanine簡介 (at) Taichung.py
Mezzanine簡介 (at) Taichung.py
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...
 
Python & Django TTT
Python & Django TTTPython & Django TTT
Python & Django TTT
 
A Basic Django Introduction
A Basic Django IntroductionA Basic Django Introduction
A Basic Django Introduction
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
Gae Meets Django
Gae Meets DjangoGae Meets Django
Gae Meets Django
 
بررسی چارچوب جنگو
بررسی چارچوب جنگوبررسی چارچوب جنگو
بررسی چارچوب جنگو
 
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
 
Staying Sane with Drupal NEPHP
Staying Sane with Drupal NEPHPStaying Sane with Drupal NEPHP
Staying Sane with Drupal NEPHP
 
Django by rj
Django by rjDjango by rj
Django by rj
 
Django Rest Framework and React and Redux, Oh My!
Django Rest Framework and React and Redux, Oh My!Django Rest Framework and React and Redux, Oh My!
Django Rest Framework and React and Redux, Oh My!
 
Django
DjangoDjango
Django
 
WRStmlDSQUmUrZpQ0tFJ4Q_a36bc57fe1a24dd8bc5ba549736e406f_C2-Week2.pptx
WRStmlDSQUmUrZpQ0tFJ4Q_a36bc57fe1a24dd8bc5ba549736e406f_C2-Week2.pptxWRStmlDSQUmUrZpQ0tFJ4Q_a36bc57fe1a24dd8bc5ba549736e406f_C2-Week2.pptx
WRStmlDSQUmUrZpQ0tFJ4Q_a36bc57fe1a24dd8bc5ba549736e406f_C2-Week2.pptx
 
Jumpstart Django
Jumpstart DjangoJumpstart Django
Jumpstart Django
 

GDG Addis - An Introduction to Django and App Engine

  • 1. Django & App Engine Developing and Deploying to the Cloud Yared Ayalew @yaredayalew
  • 2. Agenda •  Django 101 •  Using Django •  Django and App Engine
  • 4.
  • 5. What is Django? “The web framework for perfectionists with deadlines” Django makes it easier to build better web apps more quickly and with less code www.djangoproject.com
  • 6. What s it for? •  Building dynamic websites •  A high level web framework •  Abstracts common problems •  Shortcuts for fast development
  • 7. The Framework •  Elegant URL Design •  Object/Relational Mapper (ORM) •  Powerful Templating System •  Automatic Admin Interface •  i18n •  caching, syndication, middleware, email, sql, modules, authentication, sessions, comments, sitemaps, gis ...
  • 8. Architecture •  MTV •  Models describe your data •  Views control what a user sees and does •  Templates are what users see
  • 10. Requirements •  Python 2.3+ •  PostgreSQL/MySQL/SQLite •  Apache + mod_python/mod_wsgi/FastCGI
  • 11. Pip •  A tool for installing and managing Python packages •  PIP (Pip installs Python) $  wget  http://pypi.python.org/packages/source/p/pip/pip-­‐1.2.1.tar.gz     $  tar  xzf  pip-­‐1.2.1.tar.gz     $  cd  pip-­‐1.2.1   $  python  setup.py  install    
  • 12. virtualenv •  A self-contained virtual environment for Python development •  Does not touch your Python installation •  Keep track of needed modules with a requirements file •  Allows to test several package versions $  pip  install  virtualenv  
  • 13. Creating a Virtualenv Create  the  virtual  environment   $  virtualenv  myenvironment     Activate  the  virtual  environment   $  cd  myenvironment   $  source  bin/activate   (myenvironment)$      
  • 14. Get Django Download it and install from http://www.djangoproject.com/download OR $  pip  install  django  
  • 16. Create A Project $  django-­‐admin.py  startproject  myproject     myproject/    manage.py    myproject/        __init__.py        settings.py        urls.py        wsgi.py      
  • 17. Running a Project $  python  manage.py  runserver     Browse to http://localhost:8000
  • 18. Projects and Apps “A project is a collection of settings for an instance of Django, including database configuration, Django-specific options and application-specific settings.” "A bundle of Django code, including models and views, that lives together in a single Python package and represents a full Django application."
  • 19. Creating Applications $  python  manage.py  startapp  myapp     myapp/      __init__.py      models.py      tests.py      views.py      
  • 20. Django Tools •  Django Shell – manipulate your models python  manage.py  shell   •  Django Server – development & debugging environment python  manage.py  server   •  SyncDb – build your database from models python  manage.py  syncdb  
  • 22. Project Configuration •  Easy configuration in file settings.py   •  Allows you to configure: •  Database connection •  Installed apps •  Template path •  Logging etc.
  • 23. Models •  Pythonclasses that represent objects in the database and is a subclass of django.db.models.Model   •  Each attribute of the model represents a database field class  Location(models.Model):                    location_description  =  models.TextField()            city  =  models.CharField("City",max_length=200,null=True)            common_name  =  models.CharField(max_length=200)            lat  =  models.FloatField("Latitude",blank=True,null=True)            lon  =  models.FloatField("Longitude",blank=True,null=True)      
  • 24. Views •  APython function that takes a web request and returns a web response. •  Response can be html, redirection, 404, image, binary stream, xml etc. from  myapp.models  import  Location     def  location_by_city(request,city):          places  =  Location.objects.filter(city=city)          return  render_to_response(‘locations/result.html’,{‘places’:places})          
  • 25. Templates •  Atext file that can generate any text based format (html, xml, cvs etc) •  Designer friendly •  Tags, variables and filters •  {%  if  %}  {%  else  %}  {%  endif  %}   •  {%  for  item  in  list  %}    {%  endfor  %}   •  {%  ifequal  %}  {%  endifequal  %}   •  {{  name|lower  }}  
  • 26. Philosophies and Limitations •  Business logic should be separated from presentation logic •  Syntax should be decoupled from HTML/XML •  Designers are assumed to be comfortable with HTML code •  Designers are assumed not to be Python programmers •  The goal is not to invent a programming language
  • 27. base.html   Templates <html>      <head>          <title>{%  block  title  %}{%  endblock  %}</title>      </head>      <body>            {%  block  content  %}  {%  endblock  %}      </body>   </html>     result.html   {%  extends  “base.html”  %}   {%  block  title  %}  Search  Result  {%  endblock%}   {%  block  content  %}            <h2>The  following  locations  are  found:</h2>            {%  for  place  in  places  %}                  <h4>{{  place.common_name  }}</h4>                  <p>  {{  place.location_description  }}  </p>                  <img  src=’{%  url  place.get_map  %}’/>            {%  endfor  %}   {%  endblock  %}    
  • 28. URLs •  A mapping between a regex url pattern and view functions •  Part of the overall application design •  defined inside urls.py   from  django.conf.urls  import  patterns,  include,  url     urlpatterns  =  patterns('',            url(r'^$',  'myapp.index'),    url(r'^places/(?P<city>w+)/)$',  'myapp.location_by_city'),              url(r'^place/(?P<location_id>d+)/$','myapp.view_place'),)    
  • 29. Forms •  Classes that represent html forms •  Allow data input, validation, error message, label etc. class  CreateLocation(forms.Form):        description  =  forms.TextField()        city  =  forms.CharField(label=”City”,max_length=200)        common_name  =  forms.CharField(max_length=200,widget=                                    forms.TextInput(attrs={‘class’:‘medium_text’}))        lat  =  forms.FloatField()        lon  =  forms.FloatField()     from  django.forms  import  models   class  LocationForm(model.ModelForm):              class  Meta:                        model=Location    
  • 30. Forms views.py     def  new_location(request):          if  request.method==‘POST’:                form  =  CreateLocation(request.POST)                if  form.is_valid():                      #  Create  a  new  location  object  and  save  it  to  the  database                      return  HttpResponseRedirect(‘/index/’)                else:                      form  =  CreateLocation()                return  render_to_response(‘new.html’,{‘form’:form})     new.html   <form  action  =  “/new/”  method=“POST”>            {%  csrf_token  %}            {{  form.as_p  }}            <input  type=“submit”  value=“Save”/>   </form>  
  • 31. Automatic Admin •  Gives you admin interface to manage your models •  Built into the framework •  To enable admin interface in urls.py uncomment from  django.contrib  import  admin   admin.autodiscover()     url(r'^admin/',  include(admin.site.urls))     #  inside  your  app  add  a  file  called  admin.py   from  django.contrib  import  admin   from  models  import  Location     admin.site.register(Location)  
  • 32. Django + App Engine
  • 33. Django App Engine •  Supports Non-relational models (NOSQL) •  No support for Django’s ImageField and ManyToManyField •  Aggregates •  Transactions •  Many-to-many relations •  QuerySet.select_related() – a queryset that follows foreign- key relationship.
  • 34. Google Cloud SQL •  Fully managed relational database based on MySQL that lives in Google’s cloud. •  Currently in beta and requires to enable billing •  Restrictions: •  100 GB size limit per instance •  No support for user defined functions •  MySQL replication is not supported •  More detail at https://developers.google.com/cloud-sql/