Framework web para perfeccionistas
com prazos
O que vamos aprender hoje?
Ok. Mas quem é você?
Abheek
Gilson
Desenvolvedor há 5 anos
Visual Basic, Java e ASP
Python há 1 ano e meio
O que é
Django é um framework
web de alto nível que foca
no desenvolvimento
rápido, limpo e design
pragmático.
Django é um framework
web de alto nível que foca
no desenvolvimento
rápido, limpo e design
pragmático.
Django é um framework
web de alto nível que foca
no desenvolvimento
rápido, limpo e design
pragmático.
Documentação
http://django.me/design
História do Django
Adrian Holovaty
@adrianholovaty
Simon Willison
@simonw
Lawrence Journal
Jacob Kaplan Moss
@jacobian
Características
Mapeamento Objeto
Relacional ORM
Geração de Formulários
Forms e ModelForms
Interface Administrativa
Django Admin
Sistema de Templates
Internacionalização e
Localização
i18n e i10n
Desenvolvimento em
Camadas
MVC vs MTV
Model
Template
View
Desenvolvimento guiado à
Testes
TDD
URLs Flexíveis
Reusabilidade
Don't Repeat Yourself
Baterias Incluídas
Auth

Sites

Messages

Sitemap

Cache

Syndication (Feed)

Staticfiles

Markup

Logging

GeoDjango (Postgis)

Localflavors...
Quem usa?
Sistema de votações da
Engitec
engipolls
Sistema de Votações

●

●

O participante poderá votar nas palestras e
minicursos do evento, e deixar o seu
comentário;
Nã...
Sistema de Votações
●

A forma de votação será com três alternativas:
–
–

Legal;

–

●

Muito bom;
Ruim.

Gerenciar o res...
Preparando o Ambiente
Ferramentas
Python 2.7
Django 1.4
Distribute
Pip & Virtualenv
dj-database-url
Unipath
Distribute

$ wget http://pythondistribute.org/distribute_setup.py
$ sudo python distribute_setup.py
Pip & Virtualenv

$ sudo easy_install pip virtualenv
Criando o ambiente

$ virtualenv --distribute --unzip-setuptools
engipolls
Ativando o ambiente

$ cd engipolls
$ source bin/activate
Unipath

$ pip install unipath==0.2.1
Django

$ pip install django==1.4.1
Criando Projeto

$ django-admin.py startproject src
engipolls/
src/
manage.py
src/
__init__.py
settings.py
urls.py
Organizando Projeto
$ cd engipolls
$ mv src/manage.py .
$ mv src/src/* src
$ rmdir src/src
engipolls/
manage.py
src/
__init__.py
settings.py
urls.py
Criando caminhos relativos

# src/settings.py
from unipath import Path
PROJECT_DIR = Path(__file__).parent
Configurando Banco de
Dados
dj-database-url

$ pip install dj-database-url==0.2.1
Banco de Dados
import dj_database_url
DATABASES = {
'default': dj_database_url.config(
default='sqlite:///' +
PROJECT_DIR....
syncdb

$ python manage.py syncdb
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creatin...
You just installed Django's auth system, which
means you don't have any superusers defined.
Would you like to create one n...
Criando a aplicação
$ python manage.py startapp talks
$ mv talks src/
src/settings.py
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'djang...
talks/tests.py
# -*- coding: utf8 -*from django.test import TestCase
class TalkUrlTest(TestCase):
def test_get_talk_form(s...
E
===========================================================
===========
ERROR: test_get_talk_form (src.talks.tests.TalkU...
Templates: 404 e 500.html

$ mkdir src/templates
$ touch src/templates/{404,500}.html
src/setttings.py

TEMPLATE_DIRS = (
PROJECT_DIR.child('templates'),
)
Creating test database for alias 'default'...
F
===========================================================
===========
FA...
Rotas
page.php
script.cgi?pageid=144
StoryPage.aspx
0,2097,1-1-30-72-707-4027,00.html
/products/new/
/products/1/details/
/about/
src/urls.py

from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'^$', 'src.talks.views.ho...
$ python manage.py test talks
Creating test database for alias 'default'...
E
============================================...
Fluxo HTTP
●

●

GET
/

●

ROOT_URLCONF

●

src.urls

●

url(r'^$',

●

home(request)

'src.talks.views.home')
Documentação
http://django.me/urls
Views
talks/views.py
# -*- coding: utf8 -*from django.http import HttpResponse
def home(request):
return HttpResponse()
$ python manage.py tests talks
Creating test database for alias 'default'...
.
-------------------------------------------...
Refatorando
# -*- coding: utf8 -*-

talks/tests.py

from django.test import TestCase
from django.core.urlresolvers import reverse

cla...
$ python manage.py test talks
Creating test database for alias 'default'...
.
--------------------------------------------...
# -*- coding: utf8 -*-

talks/tests.py

from django.test import TestCase
from django.core.urlresolvers import reverse
clas...
$ python manage.py test talks
Creating test database for alias 'default'...
.
--------------------------------------------...
talks/tests.py
# -*- coding: utf8 -*from django.test import TestCase
from django.core.urlresolvers import reverse
class Ta...
.F
===========================================================
===========
FAIL: test_template (src.talks.tests.TalkUrlTes...
# -*- coding: utf8 -*from django.http import HttpResponse
from django.template import loader, Context

def home(request):
...
Creating test database for alias 'default'...
EE
===========================================================
===========
E...
Django Templates
Carregando templates
src/settings.py
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories...
app_directories.Loader

# crie a pasta templates e subpasta talks na
aplicação
$ mkdir -p src/talks/templates/talks
$ touc...
$ python manage.py test talks
Creating test database for alias 'default'...
..
-------------------------------------------...
Django Models
O que é Models?
Exemplo de Model

class Talk(models.Model):
name = models.CharField(max_length=100)
resume = models.TextField()
at = model...
Agora é só fazer
Agora é só fazer
mas criar o teste, é claro!
import datetime
# …
from .models import Talk
class TalkModelTest(TestCase):
def setUp(self):
self.talk = Talk.objects.crea...
$ python manage.py test talks
Traceback (most recent call last):
...
File
"/home/gilson/Projects/engipolls/src/talks/tests...
talks/models.py

class Talk(models.Model):
name = models.CharField(max_length=100)
resume = models.TextField()
at = models...
$ python manage.py test talks
Creating test database for alias 'default'...
...
------------------------------------------...
# -*- coding: utf8 -*import datetime
# …
from .models import Talk
class TalkModelTest(TestCase):
# ...
def test_unicode(se...
Creating test database for alias 'default'...
.F..
===========================================================
===========...
talks/models.py
class Talk(models.Model):
name = models.CharField(max_length=100)
resume = models.TextField()
at = models....
$ python manage.py test talks
Creating test database for alias 'default'...
....
-----------------------------------------...
Django Models
Meta options
talks/models.py
class Talk(models.Model):
# ...
class Meta:
verbose_name = u”Palestra”
db_table = “talks”
ordering = ['at,...
Minicurso
●

Dados para cadastro:
–

Nome do minicurso;

–

Resumo;

–

Hora do minicurso;

–

Palestrante.
Palestra == Minicurso
talks/tests.py
class TalkModelTest(TestCase):
def setUp(self):
self.talk = Talk.objects.create(
name=u”O que é Python?”,
r...
=============================================================
=========
ERROR: test_create (src.talks.tests.TalkModelTest)...
class Talk(models.Model):

talks/models.py

TALK_CHOICES = (
('P', u'Palestra'),
('M', u'Minicurso'),
)
# ...
type_talk = ...
$ python manage.py test talks
Creating test database for alias 'default'...
....
-----------------------------------------...
Atualizando banco
$ python manage.py syncdb
Creating tables ...
Creating table talks_talk
Installing custom SQL ...
Instal...
Votação
●

Dados para cadastro:
–

Palestra/Minicurso;

–

Tipo de votação (Muito bom, legal e ruim);

–

Comentários;
talks/models.py
class Poll(models.Model):
POLL_CHOICES = (
(u'B', u'Muito Bom'),
(u'L', u'Legal'),
(u'R', u'Ruim'),
)
talk...
Documentação
http://django.me/models
http://django.me/metaoptions
Querysets
Demonstração
Painel Administrativo
Django Admin
src/settings.py
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'djang...
src/urls.py
# ...
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# ...
url(r'^admin/', i...
Atualizando banco
$ python manage.py syncdb
Creating tables ...
Creating table django_admin_log
Installing custom SQL ...
...
Inserindo talk no Admin
talks/admin.py
# -*- coding: utf8 -*from django.contrib import admin
from .models import Talk
admin.site.register(Talk)
$ python manage.py runserver
Validating models...
0 errors found
Django version 1.4.1, using settings
'src.settings'
Devel...
Internacionalização
i18n
src/settings.py

TIME_ZONE = 'America/Sao_Paulo'
LANGUAGE_CODE = 'pt-br'
talks/models.py
# …
from django.utils.translation import ugettext as _
class Talk(models.Model):
TALK_CHOICES = (
('P', _(...
talks/models.py

# …
class Talk(models.Model):

name = models.CharField(_(u'Nome da Sessão'),
max_length=100)
resume = mod...
Django Admin
Filtros
talks/admin.py
# -*- coding: utf8 -*from django.contrib import admin
from .models import Talk
class TalkAdmin(admin.ModelA...
Django Admin
Pesquisa
talks/admin.py
# -*- coding: utf8 -*from django.contrib import admin
from .models import Talk
class TalkAdmin(admin.ModelA...
Django Admin
List Display
talks/admin.py
# -*- coding: utf8 -*from django.contrib import admin
from .models import Talk
class TalkAdmin(admin.ModelA...
Documentação
http://django.me/admin
talks/views.py
# -*- coding: utf8 -*from django.shortcuts import render
def home(request):
return render(request, 'talks/t...
$ python manage.py test talks
Creating test database for alias 'default'...
....
-----------------------------------------...
talks/views.py
# -*- coding: utf8 -*from django.shortcuts import render
from .models import Talk
def home(request):
talks ...
<!DOCTYPE HTML>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<title>Engipolls</title>
</head>
<body>
<h1>Lista de Pal...
src/templates/base.html
<!DOCTYPE HTML>

<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<title>Engipolls</title>
</head...
talks_list.html
{% extends “base.html” %}
{% block content %}
<h1>Lista de Palestras e Minicursos</h1>
{% for talk in talk...
talks_list.html
{% for talk in talks %}
<h3>{{ talk.name }}</h3>
<p>{{ talk.resume }}</p>
<p>
Horário: {{ talk.at|date:"d/...
Formulários
Django Forms
Criando arquivo

$ touch src/talks/forms.py
Formulários
Exemplo
talks/forms.py
# -*- coding: utf8 -*from django import forms

class TalkForm(forms.Form):
TALK_CHOICES = (
('P', _(u'Pales...
talks/forms.py
# -*- coding: utf8 -*from django import forms
from .models import Talk
class TalkForm(forms.Form):
TALK_CHO...
Formulários
Criando PollForm
talks/forms.py
# -*- coding: utf8 -*from django import forms
from .models import Poll
class PollForm(forms.Form):
class Me...
talks/urls.py
urlpatterns = patterns('',
# ...
url(r'^votacao/(?P<talk_id>[d]+)/$',
'src.talks.views.poll_form',
name='pol...
talks/views.py
# -*- coding: utf8 -*from django import forms
from .forms import PollForm
def poll_form(request, talk_id):
...
templates/talks/polls_form.ht
ml
{% extends “base.html” %}
{% block content %}
<form action="." method="POST">
{% csrf_tok...
talks/forms.py
# -*- coding: utf8 -*from django import forms
from .models import Poll
class PollForm(forms.Form):
class Me...
talks/views.py
# …
from django.shortcuts import get_object_or_404
from .models import Talk
def poll_form(request, talk_id)...
talks/views.py
# …
from django.shortcuts import redirect
if form.is_valid():
poll = form.save(commit=False)
poll.talk = ta...
talks_list.html
{% for talk in talks %}
<h3>{{ talk.name }}</h3>
<p>{{ talk.resume }}</p>
<p>
Horário: {{ talk.at|date:"d/...
talks/urls.py
urlpatterns = patterns('',
# ...
url(r'^votacoes/sucesso/$',
'src.talks.views.success',
name='success'),
)
talks/views.py

# …
def success(request):
return render(request,
'talks/polls_success.html', {})
templates/talks/polls_success
.html
{% extends “base.html” %}
{% block content %}
<h2>Sua votação foi efetuada com sucesso...
Dicas Finais
http://welcometothedjango.com.br
Desenvolvimento Ágil com Python e
Django
http://docs.djangoproject.com
https://github.com/nathanborror/django-basic-ap
Obrigado!
contato@gilsondev.com
@gilsonfilho
blog.gilsondev.com
http://github.com/gilsondev
Engitec - Minicurso de Django
Engitec - Minicurso de Django
Engitec - Minicurso de Django
Engitec - Minicurso de Django
Engitec - Minicurso de Django
Engitec - Minicurso de Django
Engitec - Minicurso de Django
Engitec - Minicurso de Django
Engitec - Minicurso de Django
Engitec - Minicurso de Django
Engitec - Minicurso de Django
Engitec - Minicurso de Django
Engitec - Minicurso de Django
Engitec - Minicurso de Django
Engitec - Minicurso de Django
Engitec - Minicurso de Django
Engitec - Minicurso de Django
Upcoming SlideShare
Loading in …5
×

Engitec - Minicurso de Django

2,347 views

Published on

Minicurso ministrado no Engitec 2013, usando a tecnologia Django.

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,347
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
23
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Engitec - Minicurso de Django

  1. 1. Framework web para perfeccionistas com prazos
  2. 2. O que vamos aprender hoje?
  3. 3. Ok. Mas quem é você?
  4. 4. Abheek
  5. 5. Gilson
  6. 6. Desenvolvedor há 5 anos Visual Basic, Java e ASP Python há 1 ano e meio
  7. 7. O que é
  8. 8. Django é um framework web de alto nível que foca no desenvolvimento rápido, limpo e design pragmático.
  9. 9. Django é um framework web de alto nível que foca no desenvolvimento rápido, limpo e design pragmático.
  10. 10. Django é um framework web de alto nível que foca no desenvolvimento rápido, limpo e design pragmático.
  11. 11. Documentação http://django.me/design
  12. 12. História do Django
  13. 13. Adrian Holovaty @adrianholovaty
  14. 14. Simon Willison @simonw
  15. 15. Lawrence Journal
  16. 16. Jacob Kaplan Moss @jacobian
  17. 17. Características
  18. 18. Mapeamento Objeto Relacional ORM
  19. 19. Geração de Formulários Forms e ModelForms
  20. 20. Interface Administrativa Django Admin
  21. 21. Sistema de Templates
  22. 22. Internacionalização e Localização i18n e i10n
  23. 23. Desenvolvimento em Camadas MVC vs MTV
  24. 24. Model Template View
  25. 25. Desenvolvimento guiado à Testes TDD
  26. 26. URLs Flexíveis
  27. 27. Reusabilidade Don't Repeat Yourself
  28. 28. Baterias Incluídas
  29. 29. Auth Sites Messages Sitemap Cache Syndication (Feed) Staticfiles Markup Logging GeoDjango (Postgis) Localflavors Security
  30. 30. Quem usa?
  31. 31. Sistema de votações da Engitec engipolls
  32. 32. Sistema de Votações ● ● O participante poderá votar nas palestras e minicursos do evento, e deixar o seu comentário; Não é preciso login;
  33. 33. Sistema de Votações ● A forma de votação será com três alternativas: – – Legal; – ● Muito bom; Ruim. Gerenciar o resultado das votações.
  34. 34. Preparando o Ambiente
  35. 35. Ferramentas Python 2.7 Django 1.4 Distribute Pip & Virtualenv dj-database-url Unipath
  36. 36. Distribute $ wget http://pythondistribute.org/distribute_setup.py $ sudo python distribute_setup.py
  37. 37. Pip & Virtualenv $ sudo easy_install pip virtualenv
  38. 38. Criando o ambiente $ virtualenv --distribute --unzip-setuptools engipolls
  39. 39. Ativando o ambiente $ cd engipolls $ source bin/activate
  40. 40. Unipath $ pip install unipath==0.2.1
  41. 41. Django $ pip install django==1.4.1
  42. 42. Criando Projeto $ django-admin.py startproject src
  43. 43. engipolls/ src/ manage.py src/ __init__.py settings.py urls.py
  44. 44. Organizando Projeto $ cd engipolls $ mv src/manage.py . $ mv src/src/* src $ rmdir src/src
  45. 45. engipolls/ manage.py src/ __init__.py settings.py urls.py
  46. 46. Criando caminhos relativos # src/settings.py from unipath import Path PROJECT_DIR = Path(__file__).parent
  47. 47. Configurando Banco de Dados
  48. 48. dj-database-url $ pip install dj-database-url==0.2.1
  49. 49. Banco de Dados import dj_database_url DATABASES = { 'default': dj_database_url.config( default='sqlite:///' + PROJECT_DIR.child('engipolls.db')) }
  50. 50. syncdb $ python manage.py syncdb
  51. 51. Creating tables ... Creating table auth_permission Creating table auth_group_permissions Creating table auth_group Creating table auth_user_user_permissions Creating table auth_user_groups Creating table auth_user Creating table django_content_type Creating table django_session Creating table django_site
  52. 52. You just installed Django's auth system, which means you don't have any superusers defined. Would you like to create one now? (yes/no): yes Username (leave blank to use 'gilson'): admin E-mail address: contato@gilsondev.com Password: Password (again): Superuser created successfully. Installing custom SQL ... Installing indexes ... Installed 0 object(s) from 0 fixture(s)
  53. 53. Criando a aplicação
  54. 54. $ python manage.py startapp talks $ mv talks src/
  55. 55. src/settings.py INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', # Uncomment the next line to enable the admin: # 'django.contrib.admin', # Uncomment the next line to enable admin documentation: # 'django.contrib.admindocs', 'src.talks',
  56. 56. talks/tests.py # -*- coding: utf8 -*from django.test import TestCase class TalkUrlTest(TestCase): def test_get_talk_form(self): '''Retorna status 200 da lista de palestras''' response = self.client.get('/') self.assertEquals(response.status_code, 200)
  57. 57. E =========================================================== =========== ERROR: test_get_talk_form (src.talks.tests.TalkUrlTest) Retorna status 200 da lista de palestras --------------------------------------------------------------------... template, origin = find_template(template_name) File "/home/gilson/Projects/engipolls/local/lib/python2.7/sitepackages/django/template/loader.py", line 138, in find_template raise TemplateDoesNotExist(name) TemplateDoesNotExist: 404.html -----------------------------------------------------------
  58. 58. Templates: 404 e 500.html $ mkdir src/templates $ touch src/templates/{404,500}.html
  59. 59. src/setttings.py TEMPLATE_DIRS = ( PROJECT_DIR.child('templates'), )
  60. 60. Creating test database for alias 'default'... F =========================================================== =========== FAIL: test_get_talk_form (src.talks.tests.TalkUrlTest) Retorna status 200 da lista de palestras --------------------------------------------------------------------... File "/home/gilson/Projects/engipolls/src/talks/tests.py", line 10, in test_get_talk_form self.assertEquals(response.status_code, 200) AssertionError: 404 != 200 ---------------------------------------------------------------------
  61. 61. Rotas
  62. 62. page.php script.cgi?pageid=144 StoryPage.aspx 0,2097,1-1-30-72-707-4027,00.html
  63. 63. /products/new/ /products/1/details/ /about/
  64. 64. src/urls.py from django.conf.urls import patterns, include, url urlpatterns = patterns('', url(r'^$', 'src.talks.views.home', name='home'), )
  65. 65. $ python manage.py test talks Creating test database for alias 'default'... E =========================================================== =========== ERROR: test_get_talk_form (src.talks.tests.TalkUrlTest) Retorna status 200 da lista de palestras --------------------------------------------------------------------... ViewDoesNotExist: Could not import src.talks.views.home. View does not exist in module src.talks.views. --------------------------------------------------------------------Ran 1 test in 0.026s
  66. 66. Fluxo HTTP ● ● GET / ● ROOT_URLCONF ● src.urls ● url(r'^$', ● home(request) 'src.talks.views.home')
  67. 67. Documentação http://django.me/urls
  68. 68. Views
  69. 69. talks/views.py # -*- coding: utf8 -*from django.http import HttpResponse def home(request): return HttpResponse()
  70. 70. $ python manage.py tests talks Creating test database for alias 'default'... . --------------------------------------------------------------------Ran 1 test in 0.014s
  71. 71. Refatorando
  72. 72. # -*- coding: utf8 -*- talks/tests.py from django.test import TestCase from django.core.urlresolvers import reverse class TalkUrlTest(TestCase): def test_get_talk_form(self): '''Retorna status 200 da lista de palestras''' response = self.client.get(reverse('home')) self.assertEquals(response.status_code,
  73. 73. $ python manage.py test talks Creating test database for alias 'default'... . --------------------------------------------------------------------Ran 1 test in 0.014s
  74. 74. # -*- coding: utf8 -*- talks/tests.py from django.test import TestCase from django.core.urlresolvers import reverse class TalkUrlTest(TestCase): def setUp(self): self.resp = self.client.get(reverse('home')) def test_get_talk_form(self): '''Retorna status 200 da lista de palestras''' self.assertEquals(self.resp.status_code, 200)
  75. 75. $ python manage.py test talks Creating test database for alias 'default'... . --------------------------------------------------------------------Ran 1 test in 0.014s
  76. 76. talks/tests.py # -*- coding: utf8 -*from django.test import TestCase from django.core.urlresolvers import reverse class TalkUrlTest(TestCase): # ... def test_template(self): '''Renderiza template para enviar na resposta''' self.assertTemplateUsed(self.resp, 'talks/talks_list.html')
  77. 77. .F =========================================================== =========== FAIL: test_template (src.talks.tests.TalkUrlTest) Usa o template talks/talks_list.html --------------------------------------------------------------------... File "/home/gilson/Projects/engipolls/local/lib/python2.7/sitepackages/django/test/testcases.py", line 741, in assertTemplateUsed self.fail(msg_prefix + "No templates used to render the response") AssertionError: No templates used to render the response -----------------------------------------------------------
  78. 78. # -*- coding: utf8 -*from django.http import HttpResponse from django.template import loader, Context def home(request): t = loader.get_template('talks/talks_list.html') c = Context() content = t.render(c) return HttpResponse(content)
  79. 79. Creating test database for alias 'default'... EE =========================================================== =========== ERROR: test_get_talk_form (src.talks.tests.TalkUrlTest) Retorna status 200 da lista de palestras ---------------------------------------------------------------------... TemplateDoesNotExist: talks/talks_list.html =========================================================== =========== ERROR: test_template (src.talks.tests.TalkUrlTest) Renderiza template para enviar na resposta --------------------------------------------------------------------... TemplateDoesNotExist: talks/talks_list.html -----------------------------------------------------------
  80. 80. Django Templates Carregando templates
  81. 81. src/settings.py TEMPLATE_LOADERS = ( 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader' , # ) 'django.template.loaders.eggs.Loader',
  82. 82. app_directories.Loader # crie a pasta templates e subpasta talks na aplicação $ mkdir -p src/talks/templates/talks $ touch src/talks/templates/talks/talks_list.html
  83. 83. $ python manage.py test talks Creating test database for alias 'default'... .. --------------------------------------------------------------------Ran 2 tests in 0.025s
  84. 84. Django Models
  85. 85. O que é Models?
  86. 86. Exemplo de Model class Talk(models.Model): name = models.CharField(max_length=100) resume = models.TextField() at = models.DateTimeField(auto_now_add=True) talker = models.TextField(max_length=80)
  87. 87. Agora é só fazer
  88. 88. Agora é só fazer mas criar o teste, é claro!
  89. 89. import datetime # … from .models import Talk class TalkModelTest(TestCase): def setUp(self): self.talk = Talk.objects.create( name=u”O que é Python?”, resume=u”Palestra sobre Python”, at=datetime.datetime.now(), talker=”Guido Van Rossum” ) def test_create(self): '''Registra a palestra corretamente''' self.assertEquals(self.talk.pk, 1)
  90. 90. $ python manage.py test talks Traceback (most recent call last): ... File "/home/gilson/Projects/engipolls/src/talks/tests.py", line 7, in <module> from .models import Talk ImportError: cannot import name Talk
  91. 91. talks/models.py class Talk(models.Model): name = models.CharField(max_length=100) resume = models.TextField() at = models.DateTimeField(auto_now_add=True) talker = models.TextField(max_length=80)
  92. 92. $ python manage.py test talks Creating test database for alias 'default'... ... --------------------------------------------------------------------Ran 3 tests in 0.027s
  93. 93. # -*- coding: utf8 -*import datetime # … from .models import Talk class TalkModelTest(TestCase): # ... def test_unicode(self): '''É usado para a representação do objeto''' self.assertEquals( u'Palestra: O que é Python?', unicode(self.talk))
  94. 94. Creating test database for alias 'default'... .F.. =========================================================== =========== FAIL: test_unicode (src.talks.tests.TalkModelTest) É usado para retornar a representação do objeto --------------------------------------------------------------------... AssertionError: u'Palestra: O que xe9 Python?' != u'Talk object' - Palestra: O que xe9 Python? + Talk object --------------------------------------------------------------------Ran 4 tests in 0.047s
  95. 95. talks/models.py class Talk(models.Model): name = models.CharField(max_length=100) resume = models.TextField() at = models.DateTimeField(auto_now_add=True) talker = models.TextField(max_length=80) def __unicode__(self): return “Palestra: %s” % (self.name,)
  96. 96. $ python manage.py test talks Creating test database for alias 'default'... .... --------------------------------------------------------------------Ran 4 tests in 0.030s
  97. 97. Django Models Meta options
  98. 98. talks/models.py class Talk(models.Model): # ... class Meta: verbose_name = u”Palestra” db_table = “talks” ordering = ['at,'] def __unicode__(self): return “Palestra: %s” % (self.name,)
  99. 99. Minicurso ● Dados para cadastro: – Nome do minicurso; – Resumo; – Hora do minicurso; – Palestrante.
  100. 100. Palestra == Minicurso
  101. 101. talks/tests.py class TalkModelTest(TestCase): def setUp(self): self.talk = Talk.objects.create( name=u”O que é Python?”, resume=u”Palestra sobre Python”, at=datetime.datetime.now(), talker=”Guido Van Rossum”, type_talk='P' ) def test_create(self): '''Registra a palestra corretamente''' self.assertEquals(self.talk.pk, 1)
  102. 102. ============================================================= ========= ERROR: test_create (src.talks.tests.TalkModelTest) Registra a palestra corretamente --------------------------------------------------------------------... TypeError: 'type_talk' is an invalid keyword argument for this function ============================================================= ========= ERROR: test_unicode (src.talks.tests.TalkModelTest) É usado para retornar a representação do objeto --------------------------------------------------------------------... TypeError: 'type_talk' is an invalid keyword argument for
  103. 103. class Talk(models.Model): talks/models.py TALK_CHOICES = ( ('P', u'Palestra'), ('M', u'Minicurso'), ) # ... type_talk = models.CharField(max_length=1, choices=TALK_CHOICES) # ... def __unicode__(self): if self.type_talk == 'P': return “Palestra: %s” % (self.name,) elif self.type_talk == 'M':
  104. 104. $ python manage.py test talks Creating test database for alias 'default'... .... --------------------------------------------------------------------Ran 4 tests in 0.033s
  105. 105. Atualizando banco $ python manage.py syncdb Creating tables ... Creating table talks_talk Installing custom SQL ... Installing indexes ... Installed 0 object(s) from 0 fixture(s)
  106. 106. Votação ● Dados para cadastro: – Palestra/Minicurso; – Tipo de votação (Muito bom, legal e ruim); – Comentários;
  107. 107. talks/models.py class Poll(models.Model): POLL_CHOICES = ( (u'B', u'Muito Bom'), (u'L', u'Legal'), (u'R', u'Ruim'), ) talk = models.ForeignKey('Talk', max_length=100) poll = models.CharField(max_length=1, choices=POLL_CHOICES) comments = models.TextField(max_length=180, blank=True) def __unicode__(self):
  108. 108. Documentação http://django.me/models http://django.me/metaoptions
  109. 109. Querysets
  110. 110. Demonstração
  111. 111. Painel Administrativo Django Admin
  112. 112. src/settings.py INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.admin', )
  113. 113. src/urls.py # ... from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', # ... url(r'^admin/', include(admin.site.urls)), )
  114. 114. Atualizando banco $ python manage.py syncdb Creating tables ... Creating table django_admin_log Installing custom SQL ... Installing indexes ... Installed 0 object(s) from 0 fixture(s)
  115. 115. Inserindo talk no Admin
  116. 116. talks/admin.py # -*- coding: utf8 -*from django.contrib import admin from .models import Talk admin.site.register(Talk)
  117. 117. $ python manage.py runserver Validating models... 0 errors found Django version 1.4.1, using settings 'src.settings' Development server is running at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
  118. 118. Internacionalização i18n
  119. 119. src/settings.py TIME_ZONE = 'America/Sao_Paulo' LANGUAGE_CODE = 'pt-br'
  120. 120. talks/models.py # … from django.utils.translation import ugettext as _ class Talk(models.Model): TALK_CHOICES = ( ('P', _(u'Palestra')), ('M', _(u'Minicurso')), ) # ...
  121. 121. talks/models.py # … class Talk(models.Model): name = models.CharField(_(u'Nome da Sessão'), max_length=100) resume = models.TextField(_(u'Resumo')) at = models.DateTimeField(_(u'Data da sessão'), auto_now_add=True) talker = models.TextField(_(u'Palestrante'), max_length=80) type_talk = models.CharField(_(u'Tipo da Sessão'), max_length=1, choices=TALK_CHOICES)
  122. 122. Django Admin Filtros
  123. 123. talks/admin.py # -*- coding: utf8 -*from django.contrib import admin from .models import Talk class TalkAdmin(admin.ModelAdmin): list_filter = ['at', 'type_talk'] admin.site.register(Talk, TalkAdmin)
  124. 124. Django Admin Pesquisa
  125. 125. talks/admin.py # -*- coding: utf8 -*from django.contrib import admin from .models import Talk class TalkAdmin(admin.ModelAdmin): list_filter = ['at', 'type_talk'] search_fields = ('name',) admin.site.register(Talk, TalkAdmin)
  126. 126. Django Admin List Display
  127. 127. talks/admin.py # -*- coding: utf8 -*from django.contrib import admin from .models import Talk class TalkAdmin(admin.ModelAdmin): list_display = ('name', 'at', 'type_talk',) list_filter = ['at', 'type_talk'] search_fields = ('name',) admin.site.register(Talk, TalkAdmin)
  128. 128. Documentação http://django.me/admin
  129. 129. talks/views.py # -*- coding: utf8 -*from django.shortcuts import render def home(request): return render(request, 'talks/talks_list.html', {})
  130. 130. $ python manage.py test talks Creating test database for alias 'default'... .... --------------------------------------------------------------------Ran 4 tests in 0.033s
  131. 131. talks/views.py # -*- coding: utf8 -*from django.shortcuts import render from .models import Talk def home(request): talks = Talk.objects.all() return render(request, 'talks/talks_list.html', { 'talks': talks, })
  132. 132. <!DOCTYPE HTML> <html lang="pt-BR"> <head> <meta charset="UTF-8"> <title>Engipolls</title> </head> <body> <h1>Lista de Palestras e Minicursos</h1> {% for talk in talks %} <h3>{{ talk.name }}</h3> <p>{{ talk.resume }}</p> <p>Horário: {{ talk.at|date:"d/M/Y" }}</p> {% endfor %} </body> </html>
  133. 133. src/templates/base.html <!DOCTYPE HTML> <html lang="pt-BR"> <head> <meta charset="UTF-8"> <title>Engipolls</title> </head> <body> {% block content %}{% endblock content %} </body> </html>
  134. 134. talks_list.html {% extends “base.html” %} {% block content %} <h1>Lista de Palestras e Minicursos</h1> {% for talk in talks %} <h3>{{ talk.name }}</h3> <p>{{ talk.resume }}</p> <p>Horário: {{ talk.at|date:"d/M/Y" }}</p> {% endfor %} {% endblock content %}
  135. 135. talks_list.html {% for talk in talks %} <h3>{{ talk.name }}</h3> <p>{{ talk.resume }}</p> <p> Horário: {{ talk.at|date:"d/M/Y" }} <a href=”#”>Votar agora</a> </p> <!-- (…) -->
  136. 136. Formulários Django Forms
  137. 137. Criando arquivo $ touch src/talks/forms.py
  138. 138. Formulários Exemplo
  139. 139. talks/forms.py # -*- coding: utf8 -*from django import forms class TalkForm(forms.Form): TALK_CHOICES = ( ('P', _(u'Palestra')), ('M', _(u'Minicurso')), ) name = forms.CharField(label=u'Nome da Sessão') resume = forms.CharField(label=u'Resumo', widget=forms.Textarea) at = forms.DateTimeField(label=u'Data da Sessão') talker = forms.CharField(label=u'Palestrante') type_talk = forms.ChoiceField(label=u'Tipo de Sessão', choices=TALK_CHOICES)
  140. 140. talks/forms.py # -*- coding: utf8 -*from django import forms from .models import Talk class TalkForm(forms.Form): TALK_CHOICES = ( ('P', _(u'Palestra')), ('M', _(u'Minicurso')), ) name = forms.CharField(label=u'Nome da Sessão') resume = forms.CharField(label=u'Resumo', widget=forms.Textarea) at = forms.DateTimeField(label=u'Data da Sessão') class Meta: model = Talk
  141. 141. Formulários Criando PollForm
  142. 142. talks/forms.py # -*- coding: utf8 -*from django import forms from .models import Poll class PollForm(forms.Form): class Meta: model = Poll
  143. 143. talks/urls.py urlpatterns = patterns('', # ... url(r'^votacao/(?P<talk_id>[d]+)/$', 'src.talks.views.poll_form', name='poll_form'), )
  144. 144. talks/views.py # -*- coding: utf8 -*from django import forms from .forms import PollForm def poll_form(request, talk_id): return render(request, 'talks/polls_form.html', { 'form': PollForm(), })
  145. 145. templates/talks/polls_form.ht ml {% extends “base.html” %} {% block content %} <form action="." method="POST"> {% csrf_token %} {{ form.as_p }} <input type="submit" value="Salvar Votação" /> </form> {% endblock content %}
  146. 146. talks/forms.py # -*- coding: utf8 -*from django import forms from .models import Poll class PollForm(forms.Form): class Meta: model = Poll exclude = ('talk',)
  147. 147. talks/views.py # … from django.shortcuts import get_object_or_404 from .models import Talk def poll_form(request, talk_id): talk = get_object_or_404(Talk, pk=talk_id) if request.method == 'POST': form = PollForm(request.POST) if form.is_valid(): poll = form.save(commit=False) poll.talk = talk poll.save() return render(request, 'talks/polls_form.html', { 'form': PollForm(), })
  148. 148. talks/views.py # … from django.shortcuts import redirect if form.is_valid(): poll = form.save(commit=False) poll.talk = talk poll.save() return redirect('/votacoes/sucesso') return render(request, 'talks/polls_form.html', { 'form': PollForm(), })
  149. 149. talks_list.html {% for talk in talks %} <h3>{{ talk.name }}</h3> <p>{{ talk.resume }}</p> <p> Horário: {{ talk.at|date:"d/M/Y" }} <a href=”{% url poll_form %}”>Votar agora</a> </p> <!-- (…) -->
  150. 150. talks/urls.py urlpatterns = patterns('', # ... url(r'^votacoes/sucesso/$', 'src.talks.views.success', name='success'), )
  151. 151. talks/views.py # … def success(request): return render(request, 'talks/polls_success.html', {})
  152. 152. templates/talks/polls_success .html {% extends “base.html” %} {% block content %} <h2>Sua votação foi efetuada com sucesso!</h2> <p>Continue com a votação <a href=”{% url home %}”>aqui</a></p> {% endblock content %}
  153. 153. Dicas Finais
  154. 154. http://welcometothedjango.com.br
  155. 155. Desenvolvimento Ágil com Python e Django
  156. 156. http://docs.djangoproject.com
  157. 157. https://github.com/nathanborror/django-basic-ap
  158. 158. Obrigado! contato@gilsondev.com @gilsonfilho blog.gilsondev.com http://github.com/gilsondev

×