Update day 16

Signed-off-by: Manuel Vergara <manuel@vergaracarmona.es>
This commit is contained in:
Manuel Vergara 2023-05-22 20:35:48 +02:00
parent 3961f0bb6c
commit ada9cedc9b
5 changed files with 35 additions and 4 deletions

View File

@ -598,11 +598,33 @@ Para rematar, vamos a poner un enlace para volver desde la página del formulari
<a href="{% url 'tareas' %}">Volver</a> <a href="{% url 'tareas' %}">Volver</a>
``` ```
![](../img/dia16_30.png) ![](../img/dia16_31.png)
## 16.11. - Editar tarea ## 16.11. - Editar tarea
En base/views.py vamos a importar una nueva clase:
```python
from django.views.generic.edit import CreateView, UpdateView
```
Y añadimos una nueva clase:
```python
class EditarTarea(UpdateView):
model = Tarea
fields = '__all__'
success_url = reverse_lazy('tareas')
```
Y podemos ir a base/urls.py para añadir una nueva página. Importamos EditarTarea y añadimos el path:
```python
path('editar-tarea/<int:pk>', EditarTarea.as_view(), name='editar-tarea')
```
Entonces, el html de editar tarea tiene que tener una nueva columna con el hipervínculo de modificar la tarea. Tenemos que añador la celda de cabecera `<th></th>` y la del enlace `<td><a href="{% url 'editar-tarea' tarea.id %}">Editar</a></td>`.
Dará este resultado:
![](../img/dia16_32.png)
## 16.12. - Eliminar tarea ## 16.12. - Eliminar tarea

View File

@ -6,6 +6,7 @@
<tr> <tr>
<th>Elementos</th> <th>Elementos</th>
<th></th> <th></th>
<th></th>
</tr> </tr>
{% for tarea in tareas %} {% for tarea in tareas %}
@ -13,6 +14,7 @@
<tr> <tr>
<td>{{ tarea.titulo }}</td> <td>{{ tarea.titulo }}</td>
<td><a href="{% url 'tarea' tarea.id %}">Ver</a></td> <td><a href="{% url 'tarea' tarea.id %}">Ver</a></td>
<td><a href="{% url 'editar-tarea' tarea.id %}">Editar</a></td>
</tr> </tr>
{% empty %} {% empty %}

View File

@ -1,9 +1,10 @@
from django.urls import path from django.urls import path
from .views import ListaPendientes, DetalleTarea, CrearTarea from .views import ListaPendientes, DetalleTarea, CrearTarea, EditarTarea
urlpatterns = [ urlpatterns = [
path('', ListaPendientes.as_view(), name='tareas'), path('', ListaPendientes.as_view(), name='tareas'),
path('tarea/<int:pk>', DetalleTarea.as_view(), name='tarea'), path('tarea/<int:pk>', DetalleTarea.as_view(), name='tarea'),
path('crear-tarea/', CrearTarea.as_view(), name='crear-tarea') path('crear-tarea/', CrearTarea.as_view(), name='crear-tarea'),
path('editar-tarea/<int:pk>', EditarTarea.as_view(), name='editar-tarea')
] ]

View File

@ -1,7 +1,7 @@
from django.shortcuts import render from django.shortcuts import render
from django.views.generic.list import ListView from django.views.generic.list import ListView
from django.views.generic.detail import DetailView from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView from django.views.generic.edit import CreateView, UpdateView
from django.urls import reverse_lazy from django.urls import reverse_lazy
from .models import Tarea from .models import Tarea
@ -23,3 +23,9 @@ class CrearTarea(CreateView):
model = Tarea model = Tarea
fields = '__all__' fields = '__all__'
success_url = reverse_lazy('tareas') success_url = reverse_lazy('tareas')
class EditarTarea(UpdateView):
model = Tarea
fields = '__all__'
success_url = reverse_lazy('tareas')

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB