edwin caldón - iered.org · diseño amigable de templates poco código como sea posible desarrollo...
TRANSCRIPT
![Page 1: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/1.jpg)
Django
http://vultur.unicauca.edu.co
Jornadas de Software Libre 2007JSL2007JSL2007
Desarrollo web en Python
Edwin Caldón
![Page 2: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/2.jpg)
Agenda
Arquitectura Ejemplo
Estructura (proyecto y aplicación)Definir Modelos Vistas Plantillas
Otros Frameworks Links
![Page 3: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/3.jpg)
Django
« Django es un framework de Web de alto nivel
en Python que estimula el desarrollo rápido y el
diseño limpio y pragmático »
![Page 4: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/4.jpg)
Framework ?
URL's limpias Componentes con bajo acoplamiento Diseño amigable de templates Poco código como sea posible
Desarrollo realmente rápido
![Page 5: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/5.jpg)
Arquitectura
![Page 6: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/6.jpg)
Lógica de Presentación
Lógica de Negocio
Lógica de Acceso a datos - Persistencia
V
C
M
V Views, URLConf(Models, Templates)
TemplatesT
M Models, Data Access
![Page 7: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/7.jpg)
Ejemplo
![Page 8: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/8.jpg)
Juego de preguntas (Trivial)
Las preguntas y los usuarios los crea un administrador. Existen diferentes categorías de preguntas. Cada usuario tiene su propio juego (esto es, responde a sus preguntas). Es obligatorio estar validado en el sistema para jugar.
![Page 9: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/9.jpg)
Estructura
![Page 10: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/10.jpg)
django-admin.py startproject trivial trivial/urls.py
urlpatterns = patterns('', (r'^pregunta/$', funcion_pregunta),). . .
trivial/settings.py
ROOT_URLCONFINSTALLED_APPS. . .
trivial/__init__.py
trivial/manage.py
![Page 11: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/11.jpg)
python manage.py startapp juego juego/views.py
juego/models.py
trivial/
juego/__init__.py
def index(request): preguntas = Pregunta.objects.all() return render_to_response('index.html', {'respuestas': respuestas, 'usuario': request.user,} ) . . .
class Pregunta(models.Model): titulo = models.CharField("Título", maxlength=200) . . .
![Page 12: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/12.jpg)
![Page 13: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/13.jpg)
![Page 14: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/14.jpg)
Definir Modelos - Models
![Page 15: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/15.jpg)
from django.db import modelsfrom django.contrib.auth.models import User class Pregunta(models.Model): titulo = models.CharField("Título", maxlength=200) texto = models.TextField("Texto de la pregunta") respuesta_1 = models.CharField(maxlength=200) respuesta_2 = models.CharField(maxlength=200) respuesta_3 = models.CharField(maxlength=200) respuesta_4 = models.CharField(maxlength=200) respuesta_correcta = models.CharField(maxlength=200) foto = models.CharField(maxlength=200) def __str__(self): return self.titulo
. . .
juego/models.py
![Page 16: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/16.jpg)
settings.py
DATABASE_ENGINE = 'postgresql'DATABASE_NAME = 'trivial'DATABASE_USER = 'postgres'DATABASE_PASSWORD = 'postgres'DATABASE_HOST = 'localhost'DATABASE_PORT = '5432'
INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.admin', 'trivial.juego',) . . .
![Page 17: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/17.jpg)
$ python manage.py sqlall juego
CREATE TABLE "juego_pregunta" (“id” serial NOT NULL PRIMARY KEY,“titulo” varchar(200) NOT NULL,“texto” varchar(200) NOT NULL,“respuesta_1” varchar(200) NOT NULL,. . .
);
![Page 18: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/18.jpg)
$ python manage.py syncdb
Creating table juego_pregunta. . .
![Page 19: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/19.jpg)
>>> from juego.models import Pregunta>>> p = Pregunta(titulo='', texto='',... respuesta_1='', respuesta_2='', . . .)>>> p.save()
![Page 20: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/20.jpg)
Database API
Operaciones CRUD Clase Modelo Tabla Instancia de la clase Registro
![Page 21: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/21.jpg)
Interfaz de administración
![Page 22: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/22.jpg)
Views - URLConf
![Page 23: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/23.jpg)
URLConf (urls.py)
Despachador de URL's
from django.contrib.auth.views import login, logoutfrom django.conf.urls.defaults import *from settings import MEDIA_ROOT
urlpatterns = patterns('', (r'^/?$', 'trivial.juego.views.index'), (r'^pregunta/(\d+)/$', 'trivial.juego.views.pregunta'), (r'^admin/', include('django.contrib.admin.urls')),. . . )
http://www.example.com/pregunta/3/
![Page 24: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/24.jpg)
views (juego/views.py)
from django.shortcuts import render_to_responsefrom django.contrib.auth.decorators import login_requiredfrom trivial.juego.models import Pregunta, Respuesta@login_required
def pregunta(request, id): pregunta = Pregunta.objects.get(id=id) try: respuesta = Respuesta.objects.get(pregunta=id, usuario=request.user) except ObjectDoesNotExist: respuesta = None return render_to_response('pregunta.html', {'pregunta': pregunta, 'respuesta': respuesta, 'tiempo': str(int(time.time())), } )
![Page 25: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/25.jpg)
Plantillas - Templates
![Page 26: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/26.jpg)
Molde (guía) que contiene variables, las cuales se reemplazan cuando se evalúa la plantilla, y tags (for, extend, filter, load, include ...), las cuales controlan la lógica del template.
![Page 27: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/27.jpg)
base.html
<html><head>
<title>{% block title %}{% endblock %}
</title></head><body>
{% block cuerpo %}{% endblock %}<div id=”footer”>
{% block footer %}(c) 2007{% endblock %}<div>
</body></html>
![Page 28: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/28.jpg)
Herencia de Templates
Permite construir un “esqueleto” que contiene todos los elementos comunes del sitio y define bloques que los hijos pueden sobreescribir
![Page 29: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/29.jpg)
pregunta.html
{% extends "base.html" %}
{% block cuerpo %}
<h2>Categoría: {{ pregunta.categoria }}</h2> <h3>{{ pregunta.titulo }}</h3>
{% if texto_error %} <p class="error">{{ texto_error }}</p> {% endif %}
<img class="foto" src="/site_media/{{ pregunta.foto }}">
<p>{{ pregunta.texto }}</p>
{% if respuesta %} <p>Ya has respondido antes a la pregunta.</p> <p>Tiempo empleado: {{ respuesta.tiempo }} segundos.</p> <p>El resultado fue {% if respuesta.resultado %} CORRECTO {% else %} INCORRECTO {% endif %} </p> {% else %} <form method="post" action="/responder/"> <input type="hidden" name="pregunta" value="{{ pregunta.id }}"> <input type="hidden" name="tiempo" value="{{ tiempo }}"> <input type="radio" value="{{ pregunta.respuesta_1 }}" name="respuesta">{{ pregunta.respuesta_1 }}<br>
. . . <br> <input type="submit" value="Responder"> </form> {% endif %}
{% endblock %}
![Page 30: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/30.jpg)
![Page 31: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/31.jpg)
![Page 32: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/32.jpg)
python manage.py runserver
settings.py
django-admin.py startproject urls.py
http://127.0.0.1:8080/pregunta/3/
urlpatterns = patterns('', (r'^pregunta/(\d+)$', funcion_pregunta),)
ROOT_URLCONF. . .
views.py
def funcion_pregunta(request,id): pregunta = Pregunta.objects.get(id=id)return render_to_response( 'pregunta.html', {'pregunta': pregunta, 'respuesta': respuesta, }
HttpResponse
![Page 33: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/33.jpg)
Otros Frameworks
![Page 34: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/34.jpg)
http://en.wikipedia.org/wiki/Comparison_of_web_application_frameworks
Proyecto Lenguaje I18N ORM Migrar DB Templates Seguridad
Struts Java X X Jakarta Tiles
Sympony PHP5 X Propel, Doctrine Plugin X Plugin
Django Python X Django ORM,SQLAlquemy
Desarrollo X ACL
Pylons Python X SQLObject,SQLAlquemy
X
TurboGears Python X SQLObject,SQLAlquemy
Kid
Ruby on Rails Ruby X, L10N ActiveRecord X ERB, RJS Plugin
![Page 35: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/35.jpg)
Cosas por ver
Filters Testing Django-LDAP Formularios OpenID Autenticación Internacionalización Ajax
![Page 36: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/36.jpg)
Links
![Page 37: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/37.jpg)
http://djangobook.com
http://djangoproject.com/documentation/
http://www.django.es [google groups]
http://davidasorey.net/static/django-tutorial/
Ben Askins and Alan Green.(2007). A Rails / Django Comparison. The Python Papers, Volume 2, Issue 2. Disponible en pythonpapers.org.
![Page 38: Edwin Caldón - iered.org · Diseño amigable de templates Poco código como sea posible Desarrollo realmente rápido. Arquitectura. Lógica de Presentación Lógica de Negocio Lógica](https://reader030.vdocuments.pub/reader030/viewer/2022041017/5ec9913d63d9c0059d7490c6/html5/thumbnails/38.jpg)
Gracias
Por su atención
Preguntas