This document provides an overview of developing and deploying Django applications to Google App Engine. It begins with an introduction to Django and how to set up a Django development environment using virtualenv and pip. It then covers common Django components like models, views, templates, URLs and forms. It concludes with a brief discussion of deploying Django applications to App Engine. The key topics covered include setting up a virtual environment for Django development, the model-view-template architecture of Django, and using Django tools and components to build an application that can be deployed to App Engine.
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
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
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."
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)
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/