Mini Curso de Django

Tags:

Preview:

DESCRIPTION

Minicurso realizado no II Congresso Acadêmico do CESMAC - Maceió/AL

Citation preview

Welcome to the Django!

What's Django?

"Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design."

from http://www.djangoproject.com/

"...and not a CMS System."

Origins...

Django Reinhardt

Origins...

Lawrence-Journal World

Origins...

Pycon 2005 - Adrian Holovaty e Simon WillisonBSD License*Django Software Foundation - 2008

* http://en.wikipedia.org/wiki/BSD_licenses

www.djangoproject.com

Skills

Convention Over Configuration Object-Relational Mapping (ORM)Very useful admin CRUDForm handlingElegant URL DesignTemplate systemCache systemi18n

Principles (...or philosophies)

DRY (Don't Repeat Yourself )Loose coupling Write less code as possibleQuick developmentExplicit is better than implicit

...and the best, is

MVC? No... MTV!

MVC vs. MTV

Model --> ModelView --> TemplateController --> View

Overview

Go to the Project!

Download

djangoproject.com/download

InstallUnzip Django-x.x.tar.gzpython setup.py install

Or... apt-get install python-django (Debian like OS)

Setting up...

Create a Project...

django-admin.py startproject my_project

Setup files

__init__.py - indicates a python package

manager.py - admin tasks

settings.py - project settings

urls.py - project urls map

Let's to do something...

Open settings.py:DATABASE_ENGINE = 'sqlite3'DATABASE_NAME = 'myblog.db'add 'django.contrib.admin'

Open urls.py:Uncomment the line: "from django.contrib... "Uncomment the line: "admin.autodiscover()... "Uncomment the line: "(r'^admin... "

Development server

python manager.py syncdb

python manager.py runserver [8000]

Others Servers...

Development Server

Apache + Mod_Python

Apache + FastCGI

Create the app

python manage.py startapp my_blog

Setup files

init.py - indicates a python package

models.py - app domain model

views.py - project controller

App x Project

App - web app that do something. E.g. XXX

Project - lot of apps and themselves settings. A project can be a lot of apps, and a app can stay in severals projects

Create the model

Open models.py and edit: from django.db import models

class Artigo(models.Model):titulo = models.CharField(max_length=100)conteudo = models.TextField()publicacao = models.DateTimeField()

Setting the model classes to admin

Open admin.py and edit: from django.contrib import adminfrom models import Artigo admin.site.register(Artigo)

Add the app to the admin

Open settings.py and add:"my_project.blog"

Re-sync the database

Re-run the development server

Setting the urls

Open the urls.py and edit:

from django.conf.urls.defaults import *

# Uncomment the next two lines to enable the admin:from django.contrib import adminadmin.autodiscover()

from my_blog.models import Artigo

urlpatterns = patterns('',(r'^$', 'django.views.generic.date_based.archive_index',{'queryset': Artigo.objects.all(), 'date_field': 'publicacao'}),(r'^admin/(.*)', admin.site.root),

)

Setting the templates

Create templates/blog/artigo_archive.html and edit:

<html><body>

<h1>Meu blog</h1>

{% for artigo in latest %}<h2>{{ artigo.titulo }}</h2>

{{ artigo.conteudo }}{% endfor %}

</body></html>

Look your app!

http://localhost:8000/

Recap...

Django is easyDjango is funDjango scalesDjango is maintainableDjango saves small kittensIt rocks - USE IT!

Sites powered by Python/Django

Contact/follow us...

leofernandesmo@gmail.comfelipe.buarque@gmail.com

Twitter:@leofernandesmo

@felipe_wally