Update day 16

Signed-off-by: Manuel Vergara <manuel@vergaracarmona.es>
This commit is contained in:
Manuel Vergara 2023-05-22 20:52:18 +02:00
parent ada9cedc9b
commit c317b46db0
5 changed files with 63 additions and 3 deletions

View File

@ -628,6 +628,49 @@ Dará este resultado:
## 16.12. - Eliminar tarea ## 16.12. - Eliminar tarea
Esto será muy parecido a lo anterior. Vayamos por documentos.
Añadimos en tarea_list.html:
```html
<th></th>
```
y:
```html
<td><a href="{% url 'eliminar-tarea' tarea.id %}">Eliminar</a></td>
```
En views.py importamos la clase DeleteView
```python
from django.views.generic.edit import CreateView, UpdateView, DeleteView
```
y añadimos una nueva clase:
```python
class EliminarTarea(DeleteView):
model = Tarea
context_object_name = 'tarea'
success_url = reverse_lazy('tareas')
```
Ahora abrimos un nuevo html que le llamaremos como el sistema lo marca por defecto, tarea_confirm_delete.html y añadimos:
```html
<a href="{% url 'tareas' %}">Volver</a>
<form method="post" action="">
{% csrf_token %}
<p>Vas a eliminar esta tarea: "{{tarea}}"</p>
<input type="submit" value="Eliminar">
</form>
```
Ahora nos queda importar EliminarTarea en urls.py y añadir el path:
```python
path('eliminar-tarea/<int:pk>', EliminarTarea.as_view(), name='eliminar-tarea')
```
Ya podemos eliminar tareas 😎
## 16.13. - Crear la lógica de Logueo / Deslogueo ## 16.13. - Crear la lógica de Logueo / Deslogueo
## 16.14. - Formulario de Logueo / Deslogueo ## 16.14. - Formulario de Logueo / Deslogueo

View File

@ -0,0 +1,8 @@
<a href="{% url 'tareas' %}">Volver</a>
<form method="post" action="">
{% csrf_token %}
<p>Vas a eliminar esta tarea: "{{tarea}}"</p>
<input type="submit" value="Eliminar">
</form>

View File

@ -7,6 +7,7 @@
<th>Elementos</th> <th>Elementos</th>
<th></th> <th></th>
<th></th> <th></th>
<th></th>
</tr> </tr>
{% for tarea in tareas %} {% for tarea in tareas %}
@ -15,6 +16,7 @@
<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> <td><a href="{% url 'editar-tarea' tarea.id %}">Editar</a></td>
<td><a href="{% url 'eliminar-tarea' tarea.id %}">Eliminar</a></td>
</tr> </tr>
{% empty %} {% empty %}

View File

@ -1,10 +1,11 @@
from django.urls import path from django.urls import path
from .views import ListaPendientes, DetalleTarea, CrearTarea, EditarTarea from .views import ListaPendientes, DetalleTarea, CrearTarea, EditarTarea, EliminarTarea
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') path('editar-tarea/<int:pk>', EditarTarea.as_view(), name='editar-tarea'),
path('eliminar-tarea/<int:pk>', EliminarTarea.as_view(), name='eliminar-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, UpdateView from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy from django.urls import reverse_lazy
from .models import Tarea from .models import Tarea
@ -29,3 +29,9 @@ class EditarTarea(UpdateView):
model = Tarea model = Tarea
fields = '__all__' fields = '__all__'
success_url = reverse_lazy('tareas') success_url = reverse_lazy('tareas')
class EliminarTarea(DeleteView):
model = Tarea
context_object_name = 'tarea'
success_url = reverse_lazy('tareas')