NO SOY SERPIENTE. SOY DJANGO! -HACHE BELTRAN
MITGlobal Startup Labs
México 2014
Lesson 1 – Big Picture, Django
http://gsl.mit.edu/program/mexico-summer-2014/
Today‘s Session
• The Big Picture
• MVC
• Django – A Web Application Framework
• Your First Django App
• Models in Django
• Lab – Django Setup, Start Tutorial
But first...
We need to implement the backend:
• A web application framework, like– MonoRail, CppCMS, Apache Click, Grails, Spring,
Stripes, multiple, Catalyst, CakePHP, Drupal, Symfony, CherryPy, Django, web2py, Ruby on Rails, Compujure
• A host, like– Heroku, Google App Engine
Web Application Framework• A framework (a.k.a. code libraries) that provides
functionality for common components in a website, web app, or web service.
• Eases coding for– Working with forms– Handling HTTP requests– Templates for common HTML layouts– URL mapping– Database communication– Session management– Site security
• Allows you to focus on design and functionality rather than small details.
Web Application Framework
The classic Web developer’s path goes something like this:
• Write a Web application from scratch.• Write another Web application from scratch.• Realize the application from step 1 shares much in
common with the application from step 2.• Refactor the code so that application 1 shares code with
application 2.• Repeat steps 2-4 several times.• Realize you’ve invented a framework.
This is precisely how Django itself was created!
Object Oriented Programming (OOP)
• Java
• C++, C#
• Python
• Perl
• Ruby
• PHP
• ?
“André no es programador”
“Nothing that surrounds us is object,all is subject.”
André Breton
Model-View-Controller (MVC)
• A way of organizing code often seen in web app frameworks
• Main idea is1. Separate the storage and manipulation of data (the
model) and the presentation of data (view)2. Use the Controller to communicate between the
model and view
• Advantages– Easier to develop and test model and view
independently– Easier for others to understand
Model-View-Controller (MVC)(news site example)
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
What is Django?
• Web application framework, written in Python
• Released 2005• Began with World Online, that needed
to rapidly develop applications for news sites. Real-world code.
• Named after gypsie jazz guitarist Django Reinhardt (1910-53)
• Follows the Model-View-Controller paradigm
Why Django?
• Fast and easy development of web applications– Modular and re-usable. Don’t Repeat Yourself (DRY)
principle– Loose coupling– Hides database details– Built-in SQLite database generation (support for others)– Only requirement is Python installation
• Active development and wide community support• Successful Django sites http://djangosites.org/• Supported by Google App Engine
What you need
• Procedural and object-oriented programming knowledge– Control structures (if, while, for)
– Data structures (lists, hashes/dictionaries)
– Variables
– Classes
– Objects
– Web development? Not really….
What you need
At its core, Django is simply a collection of libraries written in the Python programming language.
• To develop a site using Django, you write Python code that uses these libraries.
• Learning Django, then, is a matter of learning how to program in Python and understanding how the Django libraries work.
• For you, learning Django will be a matter of learning Django’s conventions and APIs.
Model Template View
Model View Controller
Django Architecture
MVC (Traditional)
MTV (Django)
“Everyone thinks of changing the world, but no one thinks of changing himself”
-Tolstoy
Model-Template-View
In Django: Model-Template-View (similar to MVC pattern)
• Model – describes database information
• Template – decides how to present information
• View – manages what information to output based on
request
Model-Template-View
View
Template Model
Send request for data
Asks the model for data
Provide Database InformationUI Layout Communicate
with Database
HandlesInformation Exchange
Model
Programming Interface
Command Prompt/ Console Python IDLE Sublime Text
__unicode__ should be __str__ in Python 3
New Project
Project Structure:Whole project in one folder (mysite)
• MySite Python package• Applications (polls)• (Database)• manage.py
• Use for interaction with your project
• MySite Python Package• __init__.py• settings.py• urls.py• wsgi.py
New Project
A project is a collection of settings for an instance of Django, including database
configuration, Django-specific options, and application-specific settings
Models
View
Template Model
Send request for data
Asks the model for data
Provide Database InformationUI Layout Communicate
with Database
HandlesInformation Exchange
What is a Model
• A Python class describing data in your application– Subclass of models.Model
• Assigns attributes to each data field that is implemented
• Avoid direct work with the database– No need to handle database connections,
timeouts, etc. Let Django do it for you. – Provides Schema for Database
Django Model Syntax
Import statements
SubClass of models.Model Class
Define fields
Can define more functions
Identifier
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
Define Type of Field• E.g. models.CharField
Define arguments of field• E.g. max_length=200
Django will automatically create
fields in database
Important Django Field Types
• BooleanField
– Checkbox
• CharField(max_length)
– Single-line textbox
• DateField
– Javascript calendar
• DateTimeField
– Javascript calendar, time picker
• DecimalField(max_digits, decimal_places)
– Decimal numbers
• EmailField
– Charfield that validates email address
• FileField• File upload, stores path
in database• FloatField
• Floating point numbers• IntegerField
Integer textbox• PositiveIntegerField
• Integer textbos for positive integers
• TextField• Multi-line textbox
Installation Guide
• Install Python (if you’re using Mac OS X, you will likely already have Python. Check in Terminal by typing ‘Python’)– https://www.python.org/
• Install Django 1.6 Official Release– https://docs.djangoproject.com/en/1.6/topics/install/#installing-official-
release• We will use SQLite as a database
– No initial setup needed• Once you verify that Django can see Python (last step in Quick
Install guide), write your first Django app:– https://docs.djangoproject.com/en/1.6/intro/tutorial01/
Today‘s Assignment
Continue Django Tutorial– 1: Basic Poll Application– 2: Admin Page– 3: Public interface– 4: Simple Interface– 5: Automated Testing– 6: Customize look and feel– Advanced: Packaging
Helpful Documentation:– Django Book
http://www.orkutscrap4u.com/app/webroot/scrap/cute_cat/54_cute_cat.jpg
http://th08.deviantart.net/fs71/150/f/2011/349/3/9/cat_paw_print_line_art_by_elemental_clan_cats-d4j63dm.jpg