Web Framework
THIAGO AVELINO

•   Trabalha com Software Livre desde 2005

•   Desenvolvedor Python desde 2006

•   Committer de alguns projetos OpenSource

    •   CentOS (Kernel)

    •   MongoDB

    •   Algumas bibliotecas Python (MongoEngine)
O QUE É DJANGO?


•   Framework web desenvolvido em Python

•   Padrão MVC

•   Ênfase em reusabilidade e conectividade

•   Desenvolvimento ágil

•   Conceito DRY

•   Open Source
ENTENDENDO O DJANGO
REQUISIÇÕES EM DJANGO
PYTHON


•   Linguagem de programação de alto nível

•   Orientada a objetos

•   Tipagem dinâmica e forte

•   Código legível

•   Indentação obrigatória

•   Open Source
CRIANDO PROJETO EM DJANGO

# django-admin.py startproject oficinadjango
# cd oficinadjango
# ls
__init__.py manage.py settings.py urls.py
# python manage.py runserver
Validating models...
0 errors found

Django version 1.2.3, using settings 'oficinadjango.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
TELA INICIAL DO DJANGO
CONFIGURANDO O PROJETO


Abra o arquivo settings.py

 DATABASE_ENGINE = ‘sqlite3’
 DATABASE_NAME = ‘app.db’
CRIANDO UMA APLICAÇÃO


# python manage.py startapp testando
# cd testando
# ls
__init__.py models.py tests.py views.py
MODELS




•   Cada class é uma table

•   São class que herdam de django.db.models.Model
CRIANDO UM MODELS
INSTALANDO APLICAÇÃO


Abra o arquivo settings.py


 INSTALED_APPS = (‘testando’)
GERANDO BANCO DE DADOS

# python manage.py syncdb
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 auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site

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 'avelino'):
E-mail address: thiago.avelino@gonow.com.br
Password:
Password (again):
Superuser created successfully.
Installing index for auth.Permission model
Installing index for auth.Group_permissions model
Installing index for auth.User_user_permissions model
Installing index for auth.User_groups model
Installing index for auth.Message model
No fixtures found.
DJANGO ADMIN


•   Interface grafica de adminstração de todas as aplicações no sistema

•   Orientada a objetos

•   Tipagem dinâmica e forte

•   Código legível

•   Indentação obrigatória

•   Open Source
VIEWS


•   Recebem as requisições dos usuário

•   Retorna uma resposta para a requisição, resposta pode ser uma página HTML ou qual quer
    outro tipo de documento.
TEMPLATE


•   Linguagem simples

•   Template tags

•   Tag Block
CONFIGURANDO TEMPLATE
Crie uma pasta chamada “template” na raiz do seu projeto.

 import os
 MEDIA_ROOT = os.path.dirname(os.path.abspath(__file__))
 TEMPLATE_DIRS = (‘os.path.abspath(os.path.join(MEDIA_ROOT,‘template’)’)
AUTENTICAÇÃO
                                       django.contrib.auth



•   Modulo de autenticação do Django

•   Usuários, grupos e permissões

•   Aplicações plugável
RSS / FEEDS
                           django.contrib.syndication




•   Criação de feeds RSS

•   Aplicações plugável
INTERNACIONALIZAÇÃO


•   Mudar idioma

•   Formato de data

    Mude o arquivo settings.py

     TIME_ZONE = ‘America/Sao_Paulo’
     LANGUAGE_CODE = ‘pt-BR’
     DATE_FORMAT = u’d/m/Y’
OBRIGADO!!! PERGUNTAS?

             Thiago Avelino
     thiago.avelino@gonow.com.br

          Twitter: @avelino0
        Blog: http://blog.avelino.us

Oficina de Django

  • 1.
  • 2.
    THIAGO AVELINO • Trabalha com Software Livre desde 2005 • Desenvolvedor Python desde 2006 • Committer de alguns projetos OpenSource • CentOS (Kernel) • MongoDB • Algumas bibliotecas Python (MongoEngine)
  • 3.
    O QUE ÉDJANGO? • Framework web desenvolvido em Python • Padrão MVC • Ênfase em reusabilidade e conectividade • Desenvolvimento ágil • Conceito DRY • Open Source
  • 4.
  • 5.
  • 6.
    PYTHON • Linguagem de programação de alto nível • Orientada a objetos • Tipagem dinâmica e forte • Código legível • Indentação obrigatória • Open Source
  • 7.
    CRIANDO PROJETO EMDJANGO # django-admin.py startproject oficinadjango # cd oficinadjango # ls __init__.py manage.py settings.py urls.py # python manage.py runserver Validating models... 0 errors found Django version 1.2.3, using settings 'oficinadjango.settings' Development server is running at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
  • 8.
  • 9.
    CONFIGURANDO O PROJETO Abrao arquivo settings.py DATABASE_ENGINE = ‘sqlite3’ DATABASE_NAME = ‘app.db’
  • 10.
    CRIANDO UMA APLICAÇÃO #python manage.py startapp testando # cd testando # ls __init__.py models.py tests.py views.py
  • 11.
    MODELS • Cada class é uma table • São class que herdam de django.db.models.Model
  • 12.
  • 13.
    INSTALANDO APLICAÇÃO Abra oarquivo settings.py INSTALED_APPS = (‘testando’)
  • 14.
    GERANDO BANCO DEDADOS # python manage.py syncdb 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 auth_message Creating table django_content_type Creating table django_session Creating table django_site 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 'avelino'): E-mail address: thiago.avelino@gonow.com.br Password: Password (again): Superuser created successfully. Installing index for auth.Permission model Installing index for auth.Group_permissions model Installing index for auth.User_user_permissions model Installing index for auth.User_groups model Installing index for auth.Message model No fixtures found.
  • 15.
    DJANGO ADMIN • Interface grafica de adminstração de todas as aplicações no sistema • Orientada a objetos • Tipagem dinâmica e forte • Código legível • Indentação obrigatória • Open Source
  • 16.
    VIEWS • Recebem as requisições dos usuário • Retorna uma resposta para a requisição, resposta pode ser uma página HTML ou qual quer outro tipo de documento.
  • 17.
    TEMPLATE • Linguagem simples • Template tags • Tag Block
  • 18.
    CONFIGURANDO TEMPLATE Crie umapasta chamada “template” na raiz do seu projeto. import os MEDIA_ROOT = os.path.dirname(os.path.abspath(__file__)) TEMPLATE_DIRS = (‘os.path.abspath(os.path.join(MEDIA_ROOT,‘template’)’)
  • 19.
    AUTENTICAÇÃO django.contrib.auth • Modulo de autenticação do Django • Usuários, grupos e permissões • Aplicações plugável
  • 20.
    RSS / FEEDS django.contrib.syndication • Criação de feeds RSS • Aplicações plugável
  • 21.
    INTERNACIONALIZAÇÃO • Mudar idioma • Formato de data Mude o arquivo settings.py TIME_ZONE = ‘America/Sao_Paulo’ LANGUAGE_CODE = ‘pt-BR’ DATE_FORMAT = u’d/m/Y’
  • 22.
    OBRIGADO!!! PERGUNTAS? Thiago Avelino thiago.avelino@gonow.com.br Twitter: @avelino0 Blog: http://blog.avelino.us