Im doing a "To Do" list in Django, the problem is that im trying to make a update in each task, but when i click the button "Actualizar" (Update), i want the form in that view comes pre fill with details of the task im updating (like title, description, status, expiring date and tags), and as all of you can see, the form comes with nothing in it.
Here is part of my code:
In "views.py"
class DetalleTarea(LoginRequiredMixin, DetailView):
model = Tarea
context_object_name = 'Tarea'
template_name = 'templates_app/app_1/detalle_tarea.html'
class ActualizarTarea(LoginRequiredMixin, UpdateView):
model = Tarea
form_class = TareaForm
template_name = 'templates_app/app_1/crea_actualiza_tarea.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
etiquetas = Etiqueta.objects.all()
context['etiquetas'] = etiquetas
estados = Tarea.estado
context['estados'] = estados
return context
def get_success_url(self):
return reverse_lazy('detalle_tarea', kwargs={'pk': self.object.pk})
In "urls.py"
from django.urls import path
from . import views
from .views import VistaLoginCustom, ListaTareas, DetalleTarea, CrearTarea, ActualizarTarea
from django.contrib.auth.views import LogoutView
urlpatterns = [
path('', views.home, name='home'),
path('login/', VistaLoginCustom.as_view(), name='login'),
path('logout/', LogoutView.as_view(next_page='login'), name='logout'),
path('lista_tareas/', ListaTareas.as_view(), name='lista_tareas'),
path('lista_tareas/tarea_n_<int:pk>/', DetalleTarea.as_view(), name='detalle_tarea'),
path('crear_tarea/', CrearTarea.as_view(), name='crear_tarea'),
path('actualiza_tarea/tarea_n_<int:pk>/', ActualizarTarea.as_view(), name='actualiza_tarea'),
]
In "detalle_tarea.html"
<div class="container">
<div class="row">
<div class="col-md-6 offset-md-3">
<div class="card mb-3">
<a href="{% url 'lista_tareas' %}" class="text-decoration-none"><svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" fill="blue"
class="bi bi-arrow-left m-2" viewBox="0 0 16 16">
<path fill-rule="evenodd"
d="M15 8a.5.5 0 0 0-.5-.5H2.707l3.147-3.146a.5.5 0 1 0-.708-.708l-4 4a.5.5 0 0 0 0 .708l4 4a.5.5 0 0 0 .708-.708L2.707 8.5H14.5A.5.5 0 0 0 15 8z" />
</svg> Volver al Listado de Tareas</a>
<div class="text-center">
<a class="btn btn-primary my-3 btn-sm" href="{% url 'actualiza_tarea' Tarea.id %}">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor"
class="bi bi-pencil-fill me-2" viewBox="0 0 16 16">
<path
d="M12.854.146a.5.5 0 0 0-.707 0L10.5 1.793 14.207 5.5l1.647-1.646a.5.5 0 0 0 0-.708l-3-3zm.646 6.061L9.793 2.5 3.293 9H3.5a.5.5 0 0 1 .5.5v.5h.5a.5.5 0 0 1 .5.5v.5h.5a.5.5 0 0 1 .5.5v.5h.5a.5.5 0 0 1 .5.5v.207l6.5-6.5zm-7.468 7.468A.5.5 0 0 1 6 13.5V13h-.5a.5.5 0 0 1-.5-.5V12h-.5a.5.5 0 0 1-.5-.5V11h-.5a.5.5 0 0 1-.5-.5V10h-.5a.499.499 0 0 1-.175-.032l-.179.178a.5.5 0 0 0-.11.168l-2 5a.5.5 0 0 0 .65.65l5-2a.5.5 0 0 0 .168-.11l.178-.178z" />
</svg> Actualizar
</a>
<a class="btn btn-danger my-3 btn-sm" href="#">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor"
class="bi bi-x-circle me-2" viewBox="0 0 16 16">
<path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z" />
<path
d="M4.646 4.646a.5.5 0 0 1 .708 0L8 7.293l2.646-2.647a.5.5 0 0 1 .708.708L8.707 8l2.647 2.646a.5.5 0 0 1-.708.708L8 8.707l-2.646 2.647a.5.5 0 0 1-.708-.708L7.293 8 4.646 5.354a.5.5 0 0 1 0-.708z" />
</svg> Borrar
</a>
<a class="btn btn-success my-3 btn-sm" href="{% url 'lista_tareas' %}">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor"
class="bi bi-file-earmark-check-fill me-2" viewBox="0 0 16 16">
<path
d="M9.293 0H4a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2V4.707A1 1 0 0 0 13.707 4L10 .293A1 1 0 0 0 9.293 0zM9.5 3.5v-2l3 3h-2a1 1 0 0 1-1-1zm1.354 4.354-3 3a.5.5 0 0 1-.708 0l-1.5-1.5a.5.5 0 1 1 .708-.708L7.5 9.793l2.646-2.647a.5.5 0 0 1 .708.708z" />
</svg> Completar
</a>
<h1 class="text-center fw-bold mt-3">Lista de Tareas de: {{ request.user }} </h1>
</div>
<h3 class="text-center"><b>Título: </b>{{Tarea}} </h3>
<ul class="m-3">
<li><b>Descripción: </b>{{Tarea.descripcion}}</li>
<li><b>Fecha de Vencimiento: </b>{{Tarea.fecha_vencimiento}}</li>
<li><b>Estado: </b>{{Tarea.estado}}</li>
<li><b> Etiquetas:</b>
{% for etiqueta in Tarea.etiqueta.all %}
<span class="badge rounded-pill bg-primary badge-lg me-2">{{ etiqueta.nombre }}</span>
{% endfor %}
</li>
</ul>
</div>
</div>
</div>
</div>
In "crea_actualiza_tarea.html"
<div class="container mt-3">
<div class="row">
<div class="col-md-6 offset-md-3">
<div class="card mb-3">
<a href="{% url 'lista_tareas' %}" class="text-decoration-none"><svg xmlns="http://www.w3.org/2000/svg"
width="30" height="30" fill="blue" class="bi bi-arrow-left m-2" viewBox="0 0 16 16">
<path fill-rule="evenodd"
d="M15 8a.5.5 0 0 0-.5-.5H2.707l3.147-3.146a.5.5 0 1 0-.708-.708l-4 4a.5.5 0 0 0 0 .708l4 4a.5.5 0 0 0 .708-.708L2.707 8.5H14.5A.5.5 0 0 0 15 8z" />
</svg> Volver al Listado de Tareas</a>
<div>
<h3 class="text-center fw-bold mb-4">Creación de Tareas </h3>
<form class="m-4" method="POST">
{% csrf_token %}
<input type="hidden" name="pk_tarea_creada" id="id_pk_tarea_creada">
<p>
<label class="form-label text-start fw-bold" for="id_titulo">Titulo de tu Tarea:</label>
<input class="form-control" type="text" name="titulo" maxlength="100" required
id="id_titulo" placeholder="Agrega un título a tu tarea">
</p>
<p>
<label class="form-label text-start fw-bold" for="id_descripcion">Descripcion de la
Tarea:</label>
<textarea class="form-control" name="descripcion" cols="40" rows="10" required
id="id_descripcion" placeholder="Agrega una descripción a tu tarea"></textarea>
</p>
<p>
<label class="form-label text-start fw-bold" for="id_estado">Estado de la Tarea:</label>
<select class="form-select" name="estado" id="id_estado">
<option value="Pendiente" selected>Pendiente</option>
<option value="En Progreso" selected>En Progreso</option>
<option value="Completada" selected>Completada</option>
</select>
</p>
<p>
<div>
<label class="form-label fw-bold" for="id_fecha_vencimiento">Fecha vencimiento de la
Tarea:</label>
<input class="form-control" type="date" name="fecha_vencimiento" required
id="id_fecha_vencimiento">
</div>
</p>
<p>
<label class="form-label text-start fw-bold" for="id_etiqueta">Escoje una o varias
Etiquetas (mantén presionado Ctrl):</label>
<select class="form-select" aria-label="Default select example" name="etiqueta" required
id="id_etiqueta" multiple>
{% for etiqueta in etiquetas %}
<option value="{{ etiqueta.id }}">{{ etiqueta.nombre }}</option>
{% endfor %}
</select>
</p>
<p>
<div class="d-grid gap-2">
<button class="btn btn-success" type="submit"value="Crear Tarea"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" class="bi bi-list-task me-3 " viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M2 2.5a.5.5 0 0 0-.5.5v1a.5.5 0 0 0 .5.5h1a.5.5 0 0 0 .5-.5V3a.5.5 0 0 0-.5-.5H2zM3 3H2v1h1V3z"/>
<path d="M5 3.5a.5.5 0 0 1 .5-.5h9a.5.5 0 0 1 0 1h-9a.5.5 0 0 1-.5-.5zM5.5 7a.5.5 0 0 0 0 1h9a.5.5 0 0 0 0-1h-9zm0 4a.5.5 0 0 0 0 1h9a.5.5 0 0 0 0-1h-9z"/>
<path fill-rule="evenodd" d="M1.5 7a.5.5 0 0 1 .5-.5h1a.5.5 0 0 1 .5.5v1a.5.5 0 0 1-.5.5H2a.5.5 0 0 1-.5-.5V7zM2 7h1v1H2V7zm0 3.5a.5.5 0 0 0-.5.5v1a.5.5 0 0 0 .5.5h1a.5.5 0 0 0 .5-.5v-1a.5.5 0 0 0-.5-.5H2zm1 .5H2v1h1v-1z"/>
</svg>Crear Tarea</button>
</div>
</p>
</form>
</div>
</div>
</div>
</div>
</div>
I have tried to pass the context of the task in the get_contet_data method and even use methods that I have read on google such as get_initial and others, but none have worked for me.
Replace your updateView with this then add the get_success_url function.