Martín Miranda

Agrega vistas en la aplicacion de persona

@@ -62,7 +62,7 @@ ROOT_URLCONF = 'capacitacion.urls' @@ -62,7 +62,7 @@ ROOT_URLCONF = 'capacitacion.urls'
62 TEMPLATES = [ 62 TEMPLATES = [
63 { 63 {
64 'BACKEND': 'django.template.backends.django.DjangoTemplates', 64 'BACKEND': 'django.template.backends.django.DjangoTemplates',
65 - 'DIRS': [], 65 + 'DIRS': [BASE_DIR/'templates'],
66 'APP_DIRS': True, 66 'APP_DIRS': True,
67 'OPTIONS': { 67 'OPTIONS': {
68 'context_processors': [ 68 'context_processors': [
@@ -14,8 +14,10 @@ Including another URLconf @@ -14,8 +14,10 @@ Including another URLconf
14 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 14 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
15 """ 15 """
16 from django.contrib import admin 16 from django.contrib import admin
17 -from django.urls import path 17 +from django.urls import path, include
18 18
19 urlpatterns = [ 19 urlpatterns = [
20 path('admin/', admin.site.urls), 20 path('admin/', admin.site.urls),
  21 + path('persona/', include('persona.urls'), name='persona'),
  22 + # path('persona/', include('organismo.urls'), name='persona')
21 ] 23 ]
  1 +from django import forms
  2 +
  3 +from persona.models import Persona
  4 +
  5 +
  6 +class PersonaForm(forms.ModelForm):
  7 + class Meta:
  8 + model = Persona
  9 + fields = ['nombre', 'apellido', 'documento_identidad', 'fecha_nacimiento']
@@ -2,7 +2,6 @@ import uuid @@ -2,7 +2,6 @@ import uuid
2 from django.db import models 2 from django.db import models
3 3
4 4
5 -  
6 class Persona(models.Model): 5 class Persona(models.Model):
7 class Meta: 6 class Meta:
8 verbose_name = 'Persona' 7 verbose_name = 'Persona'
  1 +{% extends 'base.html' %}
  2 +{% block titulo %}
  3 + Creacion de Persona
  4 +{% endblock %}
  5 +
  6 +{% block contenido %}
  7 +<h1>Creacion de Persona</h1>
  8 +
  9 +<form method="POST">
  10 + {{ form }}
  11 + {% csrf_token %}
  12 + <input type="submit" value="Guardar">
  13 +</form>
  14 +
  15 +
  16 +{% endblock %}
  1 +{% extends 'base.html' %}
  2 +{% block titulo %}
  3 + Detalle de Persona
  4 +{% endblock %}
  5 +
  6 +{% block contenido %}
  7 +<h1>Detalle de Persona</h1>
  8 +<ul>
  9 + <li>Nombre: {{ persona }}</li>
  10 + <li>Documento: {{ persona.documento_identidad }}</li>
  11 +</ul>
  12 +
  13 +{% endblock %}
  1 +{% extends 'base.html' %}
  2 +{% block titulo %}
  3 + Lista de Personas
  4 +{% endblock %}
  5 +
  6 +{% block contenido %}
  7 +<h1>Lista de Personas</h1>
  8 +<table>
  9 + <tr>
  10 + <th>Nombre</th>
  11 + <th>Documento</th>
  12 + </tr>
  13 + {% for persona in personas %}
  14 + <tr>
  15 + <td>{{persona}}</td>
  16 + <td>{{persona.documento_identidad}}</td>
  17 + </tr>
  18 + {% endfor %}
  19 +</table>
  20 +{% endblock %}
  1 +from django.urls import path
  2 +
  3 +from persona.views import persona_lista, persona_detalle, PersonaCreateView
  4 +
  5 +urlpatterns = [
  6 + path('lista', persona_lista, name='persona-lista'),
  7 + path('detalle/<int:pk>', persona_detalle, name='persona-detalle'),
  8 + path('creacion', PersonaCreateView.as_view(), name='persona-creacion')
  9 +]
1 -from django.shortcuts import render 1 +from django.contrib.auth.mixins import LoginRequiredMixin
  2 +from django.shortcuts import render, get_object_or_404
2 3
3 # Create your views here. 4 # Create your views here.
  5 +from django.urls import reverse_lazy
  6 +from django.views.generic import CreateView
  7 +
  8 +from persona.forms import PersonaForm
  9 +from persona.models import Persona
  10 +
  11 +
  12 +def persona_lista(request):
  13 + personas = Persona.objects.all()
  14 + return render(request, 'persona/lista_personas.html', {'personas': personas})
  15 +
  16 +
  17 +def persona_detalle(request, pk):
  18 + persona = get_object_or_404(Persona, id=pk)
  19 + return render(request, 'persona/detalle_persona.html', {'persona': persona})
  20 +
  21 +
  22 +class PersonaCreateView(LoginRequiredMixin, CreateView):
  23 + model = Persona
  24 + form_class = PersonaForm
  25 + template_name = 'persona/crear.html'
  26 + success_url = reverse_lazy('persona-lista')
  27 +
  1 +<!DOCTYPE html>
  2 + <html>
  3 + <head>
  4 + <title>{% block titulo %}{% endblock %}</title>
  5 + </head>
  6 + <body>
  7 + {% block contenido %}{% endblock %}
  8 + </body>
  9 + </html>