Introducción a Django
-
Upload
julio-galvez -
Category
Technology
-
view
443 -
download
1
Transcript of Introducción a Django
![Page 1: Introducción a Django](https://reader031.fdocuments.ec/reader031/viewer/2022021418/58edfd8b1a28abe4388b46bd/html5/thumbnails/1.jpg)
![Page 2: Introducción a Django](https://reader031.fdocuments.ec/reader031/viewer/2022021418/58edfd8b1a28abe4388b46bd/html5/thumbnails/2.jpg)
![Page 3: Introducción a Django](https://reader031.fdocuments.ec/reader031/viewer/2022021418/58edfd8b1a28abe4388b46bd/html5/thumbnails/3.jpg)
Características
• Framework web• Escrito en Python• Desarrollo rápido• Limpio• DRY• MTV• Admin automático
![Page 4: Introducción a Django](https://reader031.fdocuments.ec/reader031/viewer/2022021418/58edfd8b1a28abe4388b46bd/html5/thumbnails/4.jpg)
Características
• No usa punto y coma• No usa llaves de apertura/cierre• Tabulaciones obligatorias• Código Legible
![Page 5: Introducción a Django](https://reader031.fdocuments.ec/reader031/viewer/2022021418/58edfd8b1a28abe4388b46bd/html5/thumbnails/5.jpg)
MTV
• Mapeo Objeto-Relacional (M)• URL's y Vistas (V)• Plantillas (T)
![Page 6: Introducción a Django](https://reader031.fdocuments.ec/reader031/viewer/2022021418/58edfd8b1a28abe4388b46bd/html5/thumbnails/6.jpg)
Primer Proyecto
django-admin startproject blog
![Page 7: Introducción a Django](https://reader031.fdocuments.ec/reader031/viewer/2022021418/58edfd8b1a28abe4388b46bd/html5/thumbnails/7.jpg)
Estructura
blog/ manage.py blog/
__init__.py settings.py urls.py wsgi.py
![Page 8: Introducción a Django](https://reader031.fdocuments.ec/reader031/viewer/2022021418/58edfd8b1a28abe4388b46bd/html5/thumbnails/8.jpg)
Sincronizar BD
python manage.py migrate
![Page 9: Introducción a Django](https://reader031.fdocuments.ec/reader031/viewer/2022021418/58edfd8b1a28abe4388b46bd/html5/thumbnails/9.jpg)
Correr servidor
python manage.py runserver
![Page 10: Introducción a Django](https://reader031.fdocuments.ec/reader031/viewer/2022021418/58edfd8b1a28abe4388b46bd/html5/thumbnails/10.jpg)
Crear Aplicación
python manage.py startapp blog
![Page 11: Introducción a Django](https://reader031.fdocuments.ec/reader031/viewer/2022021418/58edfd8b1a28abe4388b46bd/html5/thumbnails/11.jpg)
Crear Aplicación
blog/ __init__.py admin.py migrations/ __init__.py models.py tests.py views.py
![Page 12: Introducción a Django](https://reader031.fdocuments.ec/reader031/viewer/2022021418/58edfd8b1a28abe4388b46bd/html5/thumbnails/12.jpg)
Tablas
Tablas = Class
No hay Sintaxis SQL
ID’s e índices automáticos
Relación por Clase, no por ID
![Page 13: Introducción a Django](https://reader031.fdocuments.ec/reader031/viewer/2022021418/58edfd8b1a28abe4388b46bd/html5/thumbnails/13.jpg)
Clases
class Articulo(models.Model): titulo = models.CharField(max_length=100) contenido = models.TextField() tags = models.TextField(blank=True, null=True)
def __str__(self): # __unicode__ on Python 2
return self.titulo
![Page 14: Introducción a Django](https://reader031.fdocuments.ec/reader031/viewer/2022021418/58edfd8b1a28abe4388b46bd/html5/thumbnails/14.jpg)
Clases
class Comentario(models.Model): articulo = models.ForeignKey(Articulo) comentario = models.TextField()
def __str__(self): # __unicode__ on Python 2
return self.comentario
![Page 15: Introducción a Django](https://reader031.fdocuments.ec/reader031/viewer/2022021418/58edfd8b1a28abe4388b46bd/html5/thumbnails/15.jpg)
Shell
python manage.py shell
![Page 16: Introducción a Django](https://reader031.fdocuments.ec/reader031/viewer/2022021418/58edfd8b1a28abe4388b46bd/html5/thumbnails/16.jpg)
Importar Modelos
from blog.models import Articulo, Comentario
![Page 17: Introducción a Django](https://reader031.fdocuments.ec/reader031/viewer/2022021418/58edfd8b1a28abe4388b46bd/html5/thumbnails/17.jpg)
Ejemplo SQL
select comentario, titulo from Comentario LEFT JOIN Articulo ON Articulo.id = Comentario.id_articulo
![Page 18: Introducción a Django](https://reader031.fdocuments.ec/reader031/viewer/2022021418/58edfd8b1a28abe4388b46bd/html5/thumbnails/18.jpg)
Ejemplo Django
c = Comentario.objects.all()
c.articulo.titulo
![Page 19: Introducción a Django](https://reader031.fdocuments.ec/reader031/viewer/2022021418/58edfd8b1a28abe4388b46bd/html5/thumbnails/19.jpg)
Ejemplos
select * from Articulo where id = 5
Articulo.objects.get(id=5)
![Page 20: Introducción a Django](https://reader031.fdocuments.ec/reader031/viewer/2022021418/58edfd8b1a28abe4388b46bd/html5/thumbnails/20.jpg)
Ejemplos
select * from Articulo where id = 5 order by titulo
Articulo.objects.filter(id=5).order_by('titulo')
![Page 21: Introducción a Django](https://reader031.fdocuments.ec/reader031/viewer/2022021418/58edfd8b1a28abe4388b46bd/html5/thumbnails/21.jpg)
Ejemplos
select * from Articulo where id > 5 order by titulo
Articulo.objects.filter(id__gt = 5).order_by('titulo')
![Page 22: Introducción a Django](https://reader031.fdocuments.ec/reader031/viewer/2022021418/58edfd8b1a28abe4388b46bd/html5/thumbnails/22.jpg)
Ejemplos
select * from Blog where id > 5 order by titulo DESC
Articulo.objects.filter(id__gt = 5).order_by('-titulo')
![Page 23: Introducción a Django](https://reader031.fdocuments.ec/reader031/viewer/2022021418/58edfd8b1a28abe4388b46bd/html5/thumbnails/23.jpg)
Ejemplo Insert
INSERT INTO Articulo (‘titulo’, ‘contenido’, ‘tags’) VALUES (‘mi libro’, ‘bla bla bla’, ‘libro, personal, bla bla’);
a = Articulo()a.titulo = ‘mi libro’a.contenido = ‘bla bla bla’a.tags = ‘libro, personal, bla bla’a.save()
![Page 24: Introducción a Django](https://reader031.fdocuments.ec/reader031/viewer/2022021418/58edfd8b1a28abe4388b46bd/html5/thumbnails/24.jpg)
Ejemplo Update
UPDATE Articulo SET titulo=‘no era mi lilbro’ WHERE id = 5
a = Articulo.objects.get(id=5)a.titulo = ‘no era mi libro’a.save()
![Page 25: Introducción a Django](https://reader031.fdocuments.ec/reader031/viewer/2022021418/58edfd8b1a28abe4388b46bd/html5/thumbnails/25.jpg)
Ejemplo Delete
DELETE FROM Articulo WHERE id = 5
a = Articulo.objects.get(id=5)a.delete()
![Page 26: Introducción a Django](https://reader031.fdocuments.ec/reader031/viewer/2022021418/58edfd8b1a28abe4388b46bd/html5/thumbnails/26.jpg)
Admin
localhost:8000/admin
![Page 27: Introducción a Django](https://reader031.fdocuments.ec/reader031/viewer/2022021418/58edfd8b1a28abe4388b46bd/html5/thumbnails/27.jpg)
admin.py
from .models import Articulo, Comentario
admin.site.register(Articulo)admin.site.register(Comentario)
![Page 28: Introducción a Django](https://reader031.fdocuments.ec/reader031/viewer/2022021418/58edfd8b1a28abe4388b46bd/html5/thumbnails/28.jpg)
Vista Post Lista
def lista_post(request): posts = Articulo.objects.all().order_by(‘id’) return render(request, ‘blog/lista.html’, locals() )
![Page 29: Introducción a Django](https://reader031.fdocuments.ec/reader031/viewer/2022021418/58edfd8b1a28abe4388b46bd/html5/thumbnails/29.jpg)
Template Lista
{% for p in posts %} <div class=‘post’> <h2> <a href=‘/detalle/{{ p.id }}’ >{{ p.titulo }}</a> </h2> <p> {{ p.contenido }} </p> </div> {% endfor %}
![Page 30: Introducción a Django](https://reader031.fdocuments.ec/reader031/viewer/2022021418/58edfd8b1a28abe4388b46bd/html5/thumbnails/30.jpg)
Vista Detalle Post
def detalle_post(request, id_post): post = Articulo.objects.get(id = id_post) comentarios = Comentario.objects.filter(articulo = post)
return render(request, ‘blog/detalle.html’, locals() )
![Page 31: Introducción a Django](https://reader031.fdocuments.ec/reader031/viewer/2022021418/58edfd8b1a28abe4388b46bd/html5/thumbnails/31.jpg)
Template Lista
<div class=‘post’> <h2>
{{ post }} </h2> <p> {{ post .contenido }} </p> </div>
{% for c in comentarios %} <p> {{ c }} </p> <hr />{% endfor %}