django & google app engine: a value composition
DESCRIPTION
Event: Giffoni Open Makers Day 2013 Speaker: Fabrizio Lapiello Questo talk è strutturato "Step by Step" per presentare in modo semplice ed intuitivo il framework Django, gli step sono incentrati sugli aspetti più appetibili e funzionali del framework fornendo informazioni di base su come utilizzarlo in ambiente Google App Engine.TRANSCRIPT
![Page 1: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/1.jpg)
![Page 2: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/2.jpg)
+
Django & App Engine a value composition
Fabrizio LapielloGDG Campania - Group Lead & Founder
![Page 3: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/3.jpg)
Step by step
● Introduzione a Django● Pennello di amministrazione● URL● Template● App Engine
![Page 4: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/4.jpg)
Step 0Introduzione a Django
![Page 5: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/5.jpg)
Introduzione a Django
● Il Framework● MTV Pattern● DBMS● Setup
○ Creazione e compilazione○ Creazione di una view
![Page 6: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/6.jpg)
Cos’è Django?
Un ottimo framework per progettare e sviluppare web app di successo.
E’ stato progettato per aumentare e migliorare la produttività nello sviluppo di web app.
www.djangoproject.com
![Page 7: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/7.jpg)
MTV - Model Template ViewModel contiene le classi le cui istanze rappresentano i dati da visualizzare e manipolare.
Template contiene gli oggetti che controllano e gestiscono l’interazione dell’utente con i livelli view e model.
View contiene i metodi usati per visualizzare tramite i template nella UI i dati contenuti nel model.
![Page 8: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/8.jpg)
DBMSAlcuni dei DBMS più noti ed utilizzati in ambiente django
![Page 9: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/9.jpg)
Conoscenze necessarie
...tutto qui!
![Page 10: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/10.jpg)
Qualche motivo...
...per imparare Python!
![Page 11: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/11.jpg)
InstallazioneDownload:http://www.djangoproject.com/download/
Guida all’installazione:http://docs.djangoproject.com/en/dev/intro/install/
Documentazione ufficiale:http://docs.djangoproject.com/
![Page 12: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/12.jpg)
Creazione e compilazione
$ django-admin.py startproject hello
$ python manage.py runserver
![Page 13: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/13.jpg)
Creazione di una vista - step 1Associare prima di tutto un URL alla vista
-> http://127.0.0.1:8000/hello
Aprire il file urls.py e inserire la seguente stringa come ultimo parametro :
-> (r’^hello/’, “views.hello”)
Espressione regolare che indica l’inizio e la fine dell’
url.
Associazione Url/Vista
![Page 14: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/14.jpg)
Creazione di una vista - step 2La vista non è altro che un metodo Python che restituisce dei dati.
Nella root del progetto creiamo il file views.py e inseriamo il seguente metodo:
from django.http import HttpResponsedef hello(request):return HttpResponse(“Hello World”)
![Page 15: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/15.jpg)
Step 1Pennello di amministrazione
![Page 16: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/16.jpg)
Pannello di amministrazione
● Creazione di Applicazioni● Creazione di Models● Aggiunta di un DB● ORM Integrato● Creazione Pannello● Models
![Page 17: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/17.jpg)
Creazione di applicazioniE’ buona abitudine strutturare il progetto in sotto-applicazioni in modo da avere maggiore controllo sull’intero sistema da sviluppare.$ python manage.py startapp test
Se non ci sono errori dovremmo avere nella root del progetto una cartella di nome “test” con all’interno i seguenti files__init__.pymodels.pyviews.py
Indicare in Installed_Apps nel file settings.py la presenza dell’applicazione “test” inserendo semplicemente il suo nome tra virgolette in fondo alla lista.
![Page 18: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/18.jpg)
Creazione di un Model
Modificare il file models.py nella cartella “test
from django.db import modelsclass Persona (models.Model):
nome = models.CharField(max_length=50)cognome = models.CharField(max_length=50)def __unicode__(self):
return u"%s %s" % (self.nome, self.cognome)class Meta:
verbose_name_plural ="Persone"
![Page 19: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/19.jpg)
Aggiunta di un DB
Modificare il file settings.py nel modo seguente:DATABASES =
{'default': {'ENGINE': 'sqlite3', #Nome del dbms es (oracle, mysql etc..)'NAME': 'test.db', #Nome del db o del percorso'USER': '', #Nome utente'PASSWORD': '', #Password'HOST': '', #Indirizzo del dbms'PORT': '', #Porta
}}
![Page 20: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/20.jpg)
ORM Integrato“Object-Relational-Mapping”
Un ORM è una sorta di linker tra gli oggetti e i database relazionali.
Attraverso questo linker gli oggetti creati con un linguaggio di alto livello vengono inseriti nel database relazionale.
$ python manage.py syncdb
![Page 21: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/21.jpg)
Creazione Pannello di Amministrazione
Aprire il file settings.py ed inserire in Installed_Apps l’applicazione per il pannello di amministrazione come di seguito.
INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.admin',)
![Page 22: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/22.jpg)
Creazione Pannello di Amministrazione
Associare un URL all’applicazione, quindi editare il file urls.py come di seguito.
from django.contrib import adminadmin.autodiscover()
urlpatterns = patterns('',(r'^admin/', include(admin.site.urls)),(r'^hello/$', "views.hello"),
)
![Page 23: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/23.jpg)
Adattiamo i nostri models
from django.contrib import admin
[...][...]
admin.site.register(Persona)
![Page 24: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/24.jpg)
Step 2URL
![Page 25: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/25.jpg)
Gli URL
● URL● Creazione di un URL
![Page 26: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/26.jpg)
URL - Uniform Resource LocatorVantaggi di avere URL “ordinati” ● Facili da ricordare ● Appetibilità da parte dei motori di ricerca ● Durevole nel tempo
Esempio pratico: 1. http://nomedominio.com/index.php?option=com_content&view=category&layout=blog&id=3&Itemid=17
2. http://nomedominio.com/blog/
1) ORRIPILANTE - ASSOLUTAMENTE DA SOSTITUIRE
![Page 27: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/27.jpg)
Creazione di un URLIn Django la creazione di un URL come il seguente
-> http://127.0.0.1:8000/hellocomporta l’associazione di una vista al suddetto URL quindi, aprire il file urls.py e inserire la seguente stringa come ultimo parametro :
-> (r’^persone/’, “views.persone”)
Espressione regolare che indica l’inizio e la fine dell’
url.
Associazione Url/Vista
![Page 28: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/28.jpg)
Una View più “complessa”from django.http import HttpResponsefrom accounting.models import *
def persone(request):elenco = ""for p in Persona.objects.all():
elenco += "%s %s <br>" % (p.nome, p.cognome)return HttpResponse(elenco)
![Page 29: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/29.jpg)
Step 3Template
![Page 30: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/30.jpg)
Un semplice templateVIEW
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.template import Context,RequestContext
def Trova(request, n):
try:
p = Persona.objects.get(nome = n)
c = Context()
c['nome']=p.nome
c['cognome']=p.cognome
return render_to_response(template.html', c, context_instance=RequestContext(request))
except:
return HttpResponse("Persona non trovata")
TEMPLATE
<h3> Dati Personali </h3>
<b>Nome:</b> {{ nome }}<br>
<b>Cognome:</b> {{ cognome }}
![Page 31: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/31.jpg)
Step 4App Engine
![Page 32: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/32.jpg)
Perchè e cos’è?
● Piattaforma PaaS● Facile da utilizzare, facilmente scalabile, traffico e
archivazioni dati proporzionati al crescere dell’esigenze
● Completamente FREE (senza costi di start-up) ● 1GB di memoria e CPU/Larghezza di banda
sufficienti a servire 5 MILIONI di pagine viste al mese.
![Page 33: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/33.jpg)
You write the code, someone else worries about everithing else…
…the power of cloud computing!
![Page 34: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/34.jpg)
Launcher
![Page 35: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/35.jpg)
App Engine Dashboard
![Page 36: Django & Google App Engine: a value composition](https://reader034.vdocument.in/reader034/viewer/2022052523/5552dd14b4c905014c8b4b40/html5/thumbnails/36.jpg)
If I can help you, scan now!