Hello everyone,
I can not find the solution that is why…
To be able to complete a todo item on my todo app I implemented the completed field in TodoItem Model. Now I want to be able to get the information form the database if the item is marked completed or not.
I thought it will be like with the item a backwards relationship and use the related object manager. But I can not make this work.
models.py:
from django.db import models
class TodoList(models.Model):
title = models.CharField(max_length=150)
def __str__(self):
return self.title
class TodoItem(models.Model):
item = models.CharField(max_length=200)
todolist = models.ForeignKey('TodoList', on_delete=models.CASCADE)
completed = models.BooleanField(default=False)
def __str__(self):
return self.item
home.html:
<div class="card-body">
<div class="card">
<table class="table table-striped">
<tbody>
{% for item in title.todoitem_set.all %}
<tr>
<td>
<a href="{% url 'edit_item' item.id %}"><i class="far fa-edit"></i></a>
<a href="{% url 'delete_item' item.id %}"><i class="far fa-trash-alt"></i></a>
<a href="#"><i class="far fa-circle"></i></a>
<a href="#"><i class="far fa-check-circle"></i></a>
</td>
<td>
{{ item }}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
I would say I need a if-else-statement in the html to diaplay the item completed or not. But how am I getting the data from database to the views and then in an it-else-statement in the html?
I am able to search for all completed items in views.py but how do I get them from views into the html? and inside the html inside into an if-else-statement?
def done(request, item_id):
# search for items which are completed (done):
done = TodoItem.objects.filter(completed=True)
return HttpResponseRedirect('/', {'done': done})
I found this kind of code but what should I do ?
Thank you
Doro