I have a project and a task model and I want to make a table in a detail html that displays the tasks in the project.
I've tried doing
<table>
<tr>
<th>Name</th>
<th>Assignee</th>
<th>Start Date</th>
<th>Due Date</th>
<th>Is compeleted</th>
</tr>
<tr>
<td>{{ task.name }} </td>
<td>{{ task.assignee }}</td>
<td>{{ task.start_date }}</td>
<td>{{ task.due_date }}</td>
<td>{{ task.is_completed }}</d>
</tr>
</table>
but it just shows the table headers and not its content
here is my task model
from django.db import models
from django.conf import settings
from django.urls import reverse
# Create your models here.
class Task(models.Model):
name = models.CharField(max_length=200)
start_date = models.DateTimeField()
due_date = models.DateTimeField()
is_completed = models.BooleanField(default=False)
project = models.ForeignKey(
"projects.Project",
related_name="tasks",
on_delete=models.CASCADE,
)
assignee = models.ForeignKey(
settings.AUTH_USER_MODEL,
null=True,
related_name="tasks",
on_delete=models.SET_NULL,
)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse("show_my_tasks")
An answer to your previous question had something like:
{% for project in projects_list %}
...
{% if project.tasks.count > 0 %}
# Displaying the table in here with the project's task info...
{% else %}
<p> no tasks for this project </p>
{% endif %}
...
{% endfor %}
You should loop over all the project's tasks set
within the table. For example:
# Within the true portion of the if statement...
{% if project.tasks.count > 0 %}
# Displaying the table in here with the project's task info...
<table>
<thead>
<tr>
<th>Name</th>
<th>Assignee</th>
<th>Start Date</th>
<th>Due Date</th>
<th>Is compeleted</th>
</tr>
</thead>
<tbody>
# The set of related tasks for the current project -> project.tasks.all
{% for task in project.tasks.all %}
<tr>
<td>{{ task.name }} </td>
<td>{{ task.assignee }}</td>
<td>{{ task.start_date }}</td>
<td>{{ task.due_date }}</td>
<td>{{ task.is_completed }}</d>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<p> no tasks for this project </p>
{% endif %}