Top Banner
NO SOY SERPIENTE. SOY DJANGO! -HACHE BELTRAN
41

-HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

Apr 28, 2018

Download

Documents

DuongAnh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

NO SOY SERPIENTE. SOY DJANGO!-HACHE BELTRAN

Page 2: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

MITGlobal Startup Labs

México 2014

Lesson 1 – Big Picture, Django

http://gsl.mit.edu/program/mexico-summer-2014/

Page 3: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

Today‘s Session

• The Big Picture

• MVC

• Django – A Web Application Framework

• Your First Django App

• Models in Django

• Lab – Django Setup, Start Tutorial

Page 4: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

Why translation?

Page 5: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

Why translation?

Page 6: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

Next week Android frontend…

Page 7: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

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

Page 8: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

Python Web Application Frameworks for Backend

Page 9: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

Google App Engine

Your Django app

Android OS

Your Android app

The Big Picture

Page 10: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,
Page 11: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

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.

Page 12: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

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!

Page 13: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

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

Page 14: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

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

Page 15: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

Model-View-Controller (MVC)

Page 16: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

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

Page 17: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

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

Page 18: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

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

Page 19: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

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….

Page 20: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

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.

Page 21: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

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

Page 22: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,
Page 23: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

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

Page 24: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

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

Page 25: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

Your First Django App

Page 26: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

Programming Interface

Command Prompt/ Console Python IDLE Sublime Text

__unicode__ should be __str__ in Python 3

Page 27: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

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

Page 28: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

New Project

A project is a collection of settings for an instance of Django, including database

configuration, Django-specific options, and application-specific settings

Page 29: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

Models

Page 30: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

Models

View

Template Model

Send request for data

Asks the model for data

Provide Database InformationUI Layout Communicate

with Database

HandlesInformation Exchange

Page 31: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

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

Page 32: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

Django Model Syntax

Import statements

SubClass of models.Model Class

Define fields

Can define more functions

Identifier

Page 33: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

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

Page 34: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

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

Page 35: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

Today‘s Assignment

Page 36: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

Poll Site

What do I need?

Page 37: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

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/

Page 38: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

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

Page 39: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

Todos saben Django!

Page 40: -HACHE BELTRAN NO SOY SERPIENTE. SOY …gsl.mit.edu/media/programs/mexico-summer-2014/materials/django_mex...•Lab – Django Setup, Start ... CherryPy, Django, web2py, Ruby on Rails,

Lorem ipsum dolor sit amet

Esta página está reservada por el gato