Martín Miranda

Agrega vistas en la aplicacion de persona

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