display boolean field info on webpage

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.


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 


<div class="card-body">
            <div class="card">
              <table class="table table-striped">
                {% for item in title.todoitem_set.all %}
                        <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>
                      {{ item }}
                {% endfor %}

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

You have:

This means that inside this loop, item is an instance of TodoItem.

Given an instance of TodoItem named item, how would you reference the completed field?

1 Like

yes, I was not using item.completed == True

Thank you very much!