Your SlideShare is downloading. ×
  • Like
Django, el framework web para perfeccionistas con deadlines
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Django, el framework web para perfeccionistas con deadlines

  • 2,236 views
Published

Presentación hecha en el Encuentro Linux 2008

Presentación hecha en el Encuentro Linux 2008

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,236
On SlideShare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
69
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Para perfeccionistas con deadlines
    • Leo Soto M.
    • Imagemaker IT
    • Encuentro Linux 2008
  • 2. ¿Python? ¿Django?
  • 3. Python is an experiment in how much freedom programmers need...
  • 4. ...Too much freedom and nobody can read another's code; too little and expressiveness is endangered
      • - Guido van Rossum
      • (Agosto 1996)
  • 5. Django
  • 6. Simple
  • 7. Flexible
  • 8. Pragmático
  • 9. Models Templates
  • 10. Models Templates Forms Auth
  • 11. Models Templates Admin Forms Auth
  • 12. Models Templates Admin Forms Auth i18n
  • 13. Models Templates Admin Forms Auth i18n GIS
  • 14. Models Templates Admin Forms Auth i18n GIS Comments DataBrowse Caching
  • 15. Models Templates Admin Forms Auth i18n GIS Comments DataBrowse Syndication Sitemaps Caching
  • 16. Models Templates Admin Forms Auth i18n GIS Comments DataBrowse Syndication Sitemaps django-evolution django-registration django-jython django-authopenid django-tagging django-command-extensions google-app-engine-django django-search django-contact-form django-contact-form django-bookmarks django-photologue django-timezones django-profile django-mptt Caching Pinax Satchmo
  • 17. Eehhh...
  • 18. Alto!
  • 19. Código > Blabla
  • 20.  
  • 21. Proyecto: “eldemo” App: “elinux”
  • 22. 1. Modelos
  • 23. # eldemo/elinux/models.py: from django.db import models from datetime import date class Noticia(models.Model): fecha = models.DateField( default=date.today) titulo = models.CharField(max_length=80) contenido = models.TextField()
  • 24. class Expositor(models.Model): nombre = models.CharField(max_length=80, unique=True) foto = models.ImageField( upload_to="fotos") resena = models.TextField(null=True, blank=True) invitado = models.BooleanField()
  • 25. class Charla(models.Model): titulo = models.CharField(max_length=120, unique=True) expositor = models.ForeignKey(Expositor)
  • 26. ¿SQL?
  • 27. ¿SQL? R: Lo genera Django
  • 28. BEGIN; CREATE TABLE "elinux_noticia" ( "id" serial NOT NULL PRIMARY KEY, "titulo" varchar(80) NOT NULL, "contenido" text NOT NULL ) ; CREATE TABLE "elinux_expositor" ( "id" serial NOT NULL PRIMARY KEY, "nombre" varchar(80) NOT NULL UNIQUE, "foto" varchar(100) NOT NULL, "resena" text NULL ) ; CREATE TABLE "elinux_charla" ( "id" serial NOT NULL PRIMARY KEY, "titulo" varchar(120) NOT NULL UNIQUE, "expositor_id" integer NOT NULL REFERENCES "elinux_expositor" ("id") DEFERRABLE INITIALLY DEFERRED ) ; CREATE INDEX "elinux_charla_expositor_id" ON "elinux_charla" ("expositor_id"); COMMIT;
  • 29. # Python: class Charla(models.Model): titulo = models.CharField(max_length=120, unique=True) expositor = models.ForeignKey(Expositor) -- SQL: CREATE TABLE "elinux_charla" ( "id" serial NOT NULL PRIMARY KEY, "titulo" varchar(120) NOT NULL UNIQUE, "expositor_id" integer NOT NULL REFERENCES "elinux_expositor" ("id") DEFERRABLE INITIALLY DEFERRED ); CREATE INDEX "elinux_charla_expositor_id" ON "elinux_charla" ("expositor_id");
  • 30. Bonus
  • 31. from django.contrib import admin from elinux.models import Noticia, Expositor, Charla admin.site.register(Expositor) admin.site.register(Charla) admin.site.register(Noticia)
  • 32.  
  • 33.  
  • 34.  
  • 35.  
  • 36.  
  • 37.  
  • 38.  
  • 39.  
  • 40. from django.contrib import admin from elinux.models import Noticia, Expositor, Charla class ExpositorAdmin(model.ModelAdmin): search_fields = ('nombre', 'resena') list_filter = ('invitado',) admin.site.register(Expositor, ExpositorAdmin ) admin.site.register(Charla) admin.site.register(Noticia)
  • 41.  
  • 42.  
  • 43.  
  • 44. from django.contrib import admin from elinux.models import Noticia, Expositor, Charla class ExpositorAdmin(model.ModelAdmin): search_fields = ('nombre', 'resena') list_filter = ('invitado',) class CharlaAdmin(model.ModelAdmin): list_display = ('titulo', 'expositor') admin.site.register(Expositor, ExpositorAdmin) admin.site.register(Charla, CharlaAdmin ) admin.site.register(Noticia)
  • 45.  
  • 46. from django.contrib import admin from elinux.models import Noticia, Expositor, Charla class ExpositorAdmin(model.ModelAdmin): search_fields = ('nombre', 'resena') list_filter = ('invitado',) class CharlaAdmin(model.ModelAdmin): list_display = ('titulo', 'expositor') class NoticiaAdmin(model.ModelAdmin): date_hierarchy = ('fecha') admin.site.register(Expositor, ExpositorAdmin) admin.site.register(Charla, CharlaAdmin) admin.site.register(Noticia, NoticiaAdmin )
  • 47.  
  • 48.  
  • 49. 2. Vistas
  • 50. URLs
  • 51. urlpatterns = patterns('eldemo.elinux.views', (r'^$', 'index'), (r'^noticias/$', 'noticias'), (r'^noticias/([0-9]+)/$', 'noticia'), (r'^expositores/invitados/$', 'expositores_invitados'), (r'^expositores/seleccionados/$', 'expositores_seleccionados') )
  • 52.  
  • 53. def index(request): noticias = Noticia.objects.all() ultimas_noticias = noticias[:3] return render_to_response( "elinux/index.html", {'noticias': ultimas_noticias})
  • 54. def noticias(request): noticias = Noticia.objects.all() return render_to_response( "elinux/noticias.html", {'noticias': noticias}) def noticia(request, id_noticia): noticia = Noticia.objects.get( id=id_noticia) return render_to_response( "elinux/noticia.html", {'noticia': noticia})
  • 55. def expositores_invitados(request): expositores = Expositor.objects.filter( invitado=True) return render_to_response( "elinux/expositores.html", {'expositores': expositores})
  • 56. 3. Templates
  • 57. Plantilla Base
  • 58. <body id=&quot;page_bg&quot; class=&quot;red&quot;> <a name=&quot;up&quot; id=&quot;up&quot;></a> <div class=&quot;center&quot;> <div id=&quot;wrapper&quot;> <div id=&quot;top&quot;> <div> <div> <span id=&quot;logo&quot; style=&quot;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='http://2008.encuentrolinux.cl/templates/linux2008/images/omt_logo_trans.png',sizingMethod='scale');&quot;></span> <span id=&quot;logo_header&quot; style=&quot;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='http://2008.encuentrolinux.cl/templates/linux2008/images/omt_logo_header.png',sizingMethod='scale');&quot;></span> <span id=&quot;joomla&quot; style=&quot;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='http://2008.encuentrolinux.cl/templates/linux2008/images/omt_joomla_trans.png',sizingMethod='scale');&quot;></span> </div> </div> </div> <div id=&quot;middle&quot;> <div id=&quot;middle_2&quot;> <div id=&quot;middle_3&quot;> <div id=&quot;middle_4&quot;> <div id=&quot;navigation&quot;> <div id=&quot;centernav&quot;> <span id=&quot;topnav&quot;> <ul id=&quot;mainlevel&quot;> <li class=&quot;red_active_menu&quot;> <a href=&quot;{% url eldemo.elinux.views.index %}&quot;>Inicio</a></li> <ul id=&quot;mainlevel&quot;> <li class=&quot;red&quot;> <a href=&quot;{% url eldemo.elinux.views.noticias %}&quot;> Noticias </a></li> <li class=&quot;red&quot;><a href=&quot;http://2008.encuentrolinux.cl/index.php?option=com_content&amp;task=view&amp;id=24&amp;Itemid=68&quot;>Inscripción</a></li> <li class=&quot;red&quot;><a href=&quot;http://2008.encuentrolinux.cl/index.php?option=com_content&amp;task=view&amp;id=13&amp;Itemid=28&quot;>Participan</a></li> <li class=&quot;red&quot;><a href=&quot;http://2008.encuentrolinux.cl/index.php?option=com_contact&amp;task=view&amp;contact_id=1&amp;Itemid=62&quot;>Contacto</a></li> </ul> </span> <div class=&quot;clr&quot;></div> </div> </div> <div id=&quot;contentarea&quot;> <table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot; class=&quot;contentarea&quot;> <tr valign=&quot;top&quot;> <td id=&quot;leftborder&quot;> <div id=&quot;pathway&quot;> <span class=&quot;pathway&quot;> <a href=&quot;http://2008.encuentrolinux.cl/index.php&quot; class=&quot;pathway&quot;>Inicio</a> <img src=&quot;http://2008.encuentrolinux.cl/templates/linux2008/images/arrow.png&quot; border=&quot;0&quot; alt=&quot;arrow&quot; /><!-- TODO: Contacto --> </span> </div> <div id=&quot;mainbody&quot;> {% block content %} {% endblock %} </div> </td> </tr> </table> </div> </div> </div> </div> </div> <div id=&quot;bottom&quot;> <div> <div>Encuentro Linux 2008 / <a href=&quot;index.php?option=com_contact&amp;Itemid=3&quot; target=&quot;_parent&quot;>Contacto</a></div> </div> </div> </div> </div> </body>
  • 59. <!-- Lo Importante: --> ... <div id=&quot;mainbody&quot;> {% block content %}{% endblock %} </div> ...
  • 60. Luego...
  • 61. <!-- index.html --> {% extends &quot;base.html&quot; %} {% block content %} ... {% for noticia in noticias %} <p> <strong>{{ noticia.titulo }}</strong> {{ noticia.contenido|truncatewords:6 }} <a href=&quot;{% url eldemo.elinux.views.noticia noticia.id %}&quot;>(ver más)</a> </p> {% endfor %} {% endblock %}
  • 62.  
  • 63. Bonus: Forms
  • 64. from django import forms class ContactForm(forms.Form): nombre = forms .CharField(max_length=200) email = forms .EmailField() titulo = forms .CharField(max_length=200) texto = forms .CharField( widget=forms.Textarea)
  • 65. def contacto(request): if request.method == 'POST': form = ContactForm(request.POST) if form.is_valid(): # TODO: Enviar el mail return HttpResponseRedirect('/') else: form = ContactForm() return render_to_response( &quot;elinux/contacto.html&quot;, {'form': form})
  • 66. {% extends &quot;base.html&quot; %} {% block content %} <form action=&quot;.&quot; method=&quot;POST&quot;> <table> {{ form.as_table }} </table> <input type=&quot;submit&quot; value=&quot;Enviar&quot; > </form> {% endblock %}
  • 67.  
  • 68.  
  • 69.  
  • 70. Más Ideas y Posibilidades...
  • 71. Ubicacion Geográfica de los Asistentes
  • 72. Comentarios
  • 73. Ejecución en la JVM (via Jython)
  • 74. Feeds
  • 75. Feeds (Oh, pero eso es demasiado fácil)
  • 76. from django.contrib.syndication.feeds import Feed from elinux.models import Noticia class NoticiasFeed(Feed): title = &quot;Noticias ELinux&quot; link = &quot;/noticias&quot; description = &quot;Noticias Encuentro Linux 2008&quot; def items(self): return Noticia.objects.all()
  • 77.  
  • 78. El framework web para perfeccionistas con deadlines 3. Templates
  • 79. ¿Preguntas?
  • 80. Gracias! leo.soto@gmail.com http://blog.leosoto.com
  • 81. Imágenes (Créditos) Desde Flickr (licenciadas vía Creative Commons): http://www.flickr.com/photos nothingpersonal/252531721/ http://www.flickr.com/photos/d_oracle/349073686/ http://www.flickr.com/photos/d_oracle/350780566/ http://www.flickr.com/photos/spine/2425394931/ http://www.flickr.com/photos/nino63004/2471663305/ http://www.flickr.com/photos/ryanricketts/2295726918/ http://www.flickr.com/photos/twatson/2854156629/ http://www.flickr.com/photos/funkybug/1538454520/ http://www.flickr.com/photos/nirak/331916210/