Mini Curso Django Ii Congresso Academico Ces

1,981 views

Published on

Apresentação usada no mini-curso de Django realizado no 2º Congresso Acadêmico do Cesmac.

  • Be the first to comment

  • Be the first to like this

Mini Curso Django Ii Congresso Academico Ces

  1. 1. Welcome to the Django!
  2. 2. What's Django? <ul><li>&quot;Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.&quot; </li></ul><ul><li>  </li></ul><ul><li>from http://www.djangoproject.com/ </li></ul>  &quot;...and not a CMS System.&quot;
  3. 3. Origins... Django Reinhardt
  4. 4. Origins... Lawrence-Journal World
  5. 5. Origins... <ul><ul><li>Pycon 2005 - Adrian Holovaty e Simon Willison </li></ul></ul><ul><ul><li>BSD License* </li></ul></ul><ul><ul><li>Django Software Foundation - 2008 </li></ul></ul>* http://en.wikipedia.org/wiki/BSD_licenses
  6. 6. www.djangoproject.com
  7. 7. Skills <ul><li>  </li></ul><ul><ul><li>Convention Over Configuration </li></ul></ul><ul><ul><li>Object-Relational Mapping (ORM) </li></ul></ul><ul><ul><li>Very useful admin CRUD </li></ul></ul><ul><ul><li>Form handling </li></ul></ul><ul><ul><li>Elegant URL Design </li></ul></ul><ul><ul><li>Template system </li></ul></ul><ul><ul><li>Cache system </li></ul></ul><ul><ul><li>i18n </li></ul></ul><ul><li>  </li></ul><ul><li>  </li></ul><ul><li>  </li></ul>
  8. 8. Principles (...or philosophies) <ul><li>  </li></ul><ul><li>  </li></ul><ul><li>  </li></ul><ul><ul><li>DRY ( Don't Repeat Yourself ) </li></ul></ul><ul><ul><li>Loose coupling </li></ul></ul><ul><ul><li>Write less code as possible </li></ul></ul><ul><ul><li>Quick development </li></ul></ul><ul><ul><li>Explicit is better than implicit </li></ul></ul><ul><li>  </li></ul><ul><li>  </li></ul><ul><li>  </li></ul><ul><li>  </li></ul>
  9. 9. <ul><li>...and the best, is </li></ul>
  10. 10. MVC? No... MTV!
  11. 11. MVC vs. MTV <ul><li>  </li></ul><ul><li>  </li></ul><ul><li>  </li></ul><ul><ul><li>M odel --> M odel </li></ul></ul><ul><ul><li>V iew --> T emplate </li></ul></ul><ul><ul><li>C ontroller --> V iew </li></ul></ul>
  12. 12. Overview
  13. 13. Go to the Project!
  14. 14. <ul><li>  </li></ul><ul><ul><li>Download </li></ul></ul><ul><ul><ul><li>djangoproject.com/download </li></ul></ul></ul><ul><ul><li>Install </li></ul></ul><ul><ul><ul><li>Unzip Django-x.x.tar.gz </li></ul></ul></ul><ul><ul><ul><li>python setup.py install </li></ul></ul></ul><ul><ul><li>Or... apt-get install python-django (Debian like OS) </li></ul></ul>Setting up...
  15. 15. Create a Project... <ul><li>  </li></ul><ul><li>  </li></ul><ul><li>  </li></ul><ul><ul><li>django-admin.py startproject my_project </li></ul></ul>
  16. 16. Setup files <ul><li>  </li></ul><ul><ul><li>__init__.py - indicates a python package </li></ul></ul><ul><li>  </li></ul><ul><ul><li>manager.py - admin tasks </li></ul></ul><ul><li>  </li></ul><ul><ul><li>settings.py - project settings </li></ul></ul><ul><li>  </li></ul><ul><ul><li>urls.py - project urls map </li></ul></ul>
  17. 17. Let's to do something... <ul><ul><li>Open settings.py : </li></ul></ul><ul><ul><ul><li>DATABASE_ENGINE = 'sqlite3' </li></ul></ul></ul><ul><ul><ul><li>DATABASE_NAME = 'myblog.db' </li></ul></ul></ul><ul><ul><ul><li>add 'django.contrib.admin' </li></ul></ul></ul><ul><ul><li>Open urls.py : </li></ul></ul><ul><ul><ul><li>Uncomment the line: &quot; from django.contrib... &quot; </li></ul></ul></ul><ul><ul><ul><li>Uncomment the line: &quot; admin.autodiscover()... &quot; </li></ul></ul></ul><ul><ul><ul><li>Uncomment the line: &quot; (r'^admin... &quot; </li></ul></ul></ul>
  18. 18. Development server <ul><li>  </li></ul><ul><li>  </li></ul><ul><li>  </li></ul><ul><ul><li>python manager.py syncdb </li></ul></ul><ul><li>  </li></ul><ul><ul><li>python manager.py runserver [8000] </li></ul></ul>
  19. 19. Others Servers... <ul><ul><li>Development Server </li></ul></ul><ul><li>  </li></ul><ul><ul><li>Apache + Mod_Python </li></ul></ul><ul><li>  </li></ul><ul><ul><li>Apache + FastCGI </li></ul></ul>
  20. 20. Create the app <ul><li>  </li></ul><ul><li>  </li></ul><ul><li>  </li></ul><ul><ul><li>python manage.py startapp my_blog </li></ul></ul>
  21. 21. Setup files <ul><li>  </li></ul><ul><li>  </li></ul><ul><ul><li>init.py - indicates a python package </li></ul></ul><ul><li>  </li></ul><ul><ul><li>models.py - app domain model </li></ul></ul><ul><li>  </li></ul><ul><ul><li>views.py - project controller </li></ul></ul>
  22. 22. App x Project <ul><li>  </li></ul><ul><li>  </li></ul><ul><ul><li>App - web app that do something. E.g. XXX </li></ul></ul><ul><ul><li>Project - lot of apps and themselves settings. A project can be a lot of apps, and a app can stay in severals projects </li></ul></ul>
  23. 23. Create the model <ul><li>  </li></ul><ul><li>  </li></ul><ul><ul><li>  Open models.py and edit: </li></ul></ul><ul><li>  </li></ul><ul><li>  </li></ul><ul><li>from django.db import models class Artigo(models.Model): </li></ul><ul><ul><li>titulo = models.CharField(max_length=100) </li></ul></ul><ul><ul><li>conteudo = models.TextField() </li></ul></ul><ul><ul><li>publicacao = models.DateTimeField() </li></ul></ul>
  24. 24. Setting the model classes to admin <ul><ul><li>Open admin.py and edit: </li></ul></ul><ul><li>  </li></ul><ul><li>  </li></ul><ul><li>  </li></ul><ul><li>from django.contrib import admin from models import Artigo   </li></ul><ul><li>admin.site.register(Artigo) </li></ul>
  25. 25. Add the app to the admin <ul><li>  </li></ul><ul><li>  </li></ul><ul><ul><li>Open settings.py and add: </li></ul></ul><ul><ul><li>&quot;my_project.blog&quot; </li></ul></ul><ul><ul><li>Re-sync the database </li></ul></ul><ul><ul><li>Re-run the development server </li></ul></ul>
  26. 26. Setting the urls <ul><li>  </li></ul><ul><ul><li>Open the urls.py and edit: </li></ul></ul><ul><li>  </li></ul><ul><li>from django.conf.urls.defaults import * # Uncomment the next two lines to enable the admin: from django.contrib import admin admin.autodiscover() from my_blog.models import Artigo urlpatterns = patterns('', </li></ul><ul><ul><ul><ul><li>(r'^$', 'django.views.generic.date_based.archive_index', </li></ul></ul></ul></ul><ul><ul><ul><ul><li>{'queryset': Artigo.objects.all(), 'date_field': 'publicacao'}), </li></ul></ul></ul></ul><ul><ul><ul><ul><li>(r'^admin/(.*)', admin.site.root), </li></ul></ul></ul></ul><ul><li>) </li></ul>
  27. 27. Setting the templates <ul><li>  </li></ul><ul><ul><li>Create templates/blog/artigo_archive.html and edit:  </li></ul></ul><ul><li>  </li></ul><ul><li>  </li></ul><ul><li><html><body> <h1>Meu blog</h1> {% for artigo in latest %} <h2>{{ artigo.titulo }}</h2> {{ artigo.conteudo }} {% endfor %} </body></html> </li></ul>
  28. 28. Look your app! <ul><li>  </li></ul><ul><li>  </li></ul><ul><li>  </li></ul><ul><ul><li>http://localhost:8000/ </li></ul></ul>
  29. 29. Recap... <ul><li>  </li></ul><ul><li>  </li></ul><ul><li>  </li></ul><ul><ul><li>Django is easy </li></ul></ul><ul><ul><li>Django is fun </li></ul></ul><ul><ul><li>Django scales </li></ul></ul><ul><ul><li>Django is maintainable </li></ul></ul><ul><ul><li>Django saves small kittens </li></ul></ul><ul><ul><li>It rocks - USE IT!  </li></ul></ul>
  30. 30. Sites powered by Python/Django
  31. 32. Contact/follow us... <ul><li>[email_address] </li></ul><ul><li>[email_address] </li></ul><ul><li>Twitter: </li></ul><ul><li>@leofernandesmo </li></ul><ul><li>@felipe_wally </li></ul>

×