Django
-
Upload
adolfo-fitoria -
Category
Documents
-
view
25.379 -
download
4
description
Transcript of Django
el framework para perfeccionistas con tiempos límite
Presentación original de Julio Acuña modificada por Adolfo FitoriaAportes de Byron Corrales.
¿chango?
Conociendo Django
● Se originó en un ambiente periodístico– Los desarrolladores deben ir al mismo ritmo
que los periodistas
● Código “mantenible” por pocos desarrolladores– Lenguaje dinámico– Código reusable en varios sitios
¿Otro framework más?
● Otras tecnologías– PHP– JSP– ASP– Rails
● Python– Zope– TurboGears– Pylons– CherryPy– Quixote– web.py– etc.
¿Por qué Django es tan bueno?
● Usa Python ;)● Desarrollo rápido y limpio● WSGI● Usa el modelo MVC ¿o será MTV?● Se basa en el principio DRY● No escribes SQL a menos que sea
absolutamente necesario● Excelente desempeño
Componentes de Django● ORM● URL Dispatcher● cache● syndication● session● auth/auth● internationalization● flatpages, redirects, csrf, comments
¿Qué necesitamos?
● Python 2.3 en adelante (rama estable)● algún manejador de bases de datos
(PostgreSQL, MySQL, sqlite3, Oracle)● el respectivo adaptador para la base de
datos● servidor web con soporte para Python
(apache2, cherokee, lighttpd )● y por supuesto Django
Descargar DjangoDescargar Django
1. Descargar paquete Django-1.1.tar.gz.http://www.djangoproject.com/download/
2. Descargar desde el manejador de paquete de tu distribución linux
# sudo apt-get install python-django
3. Descargar django desde el trunk
svn co http://code.djangoproject.com/svn/django/trunk/ django_src
INSTALEMOS DJANGO
# sudo python setup.py install
Empieza la magia
django-admin.py startproject demo
EstructuraEstructura
__init__.py
manage.py
settings.py
url.py
settings.py
DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = 'demo.sqlite3'
DATABASE_USER = ''
DATABASE_PASSWORD = ''
DATABASE_HOST = ''
DATABASE_PORT = ''
settings.py
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'demo.blog',
)
La magia continúa
django-admin.py startapp blog
models.py
from django.db import models
class Post(models.Model): title=models.CharField(max_length=50) slug = models.SlugField(unique=True) date=models.DateTimeField('published on') body = models.TextField('Body', blank=True) def __str__(self): return self.title
La magia continúa
python manage.py syncdb
El Equivalente SQLiteEl Equivalente SQLite
CREATE TABLE "blog__categoria" ( "id" integer NOT NULL PRIMARY KEY, "nombre" varchar(50) NOT NULL);CREATE TABLE "blog__entrada" ( "id" integer NOT NULL PRIMARY KEY, "titulo" varchar(50) NOT NULL, "fecha" datetime NOT NULL, "categoria_id" integer NOT NULL REFERENCES "blog__categoria" ("id"), "contenido" text NOT NULL);
views.py
from django.shortcuts import render_to_response
from models import Post
def index(request):
entries = Post.objects.all()
return render_to_response("blog/index.html",
{"entries": entries})
http://www.drugemporium.com/cstage?eccookie=@eccookie@&ecaction=de_ecwalkin&template=de_walkin.en.htm
http://www.amazon.com/Iron-Maiden-Flight-666-Deluxe/dp/B001W63DX2/ref=sr_1_1?ie=UTF8&s=music&qid=1243516729&sr=8-1
¿Qué es eso?
¡Es horríble!
http://www.misitio.com/blog/
Mucho mejor
blog/urls.py
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^$', 'blog.views.front'),
)
urls.pyfrom django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^mysite/', include('mysite.foo.urls')),
# Uncomment this for admin:
(r'^admin/', include('django.contrib.admin.urls')),
(r'^blog/', include('demo.blog.urls')),
)
admin.py
from django.contrib import admin
from models import Post
class PostAdmin(admin.ModelAdmin):
fields = ('title', 'slug', 'date', 'body')
prepopulated_fields = {"slug": ("title",)}
admin.site.register(Post, PostAdmin)
base.html<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/2002/REC-xhtml1-20020801/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head><title></title></head>
<body>
<div id='content'>
{% block content %}{% endblock %}
</div>
</body>
</html>
blog.html
● {% extends "base.html" %}● {% block content %}● {% for entry in entries %}● <h2>{{entry.title}}</h2>● <h3>{{entry.date|date:"j/n/Y
H:i"}}</h3>● {{entry.body}}● {% endfor %}● {% endblock %}
Recursos
● http://djangoproject.com● http://djangobook.org● http://djangosearch.com● http://djangosites.org● http://djangogigs.com● http://djangosnippets.org● http://django.es● http://djangopeople.net
Gracias