title: Listing my tasks under my projects in Django
body: <p>I am new to Django (and programming in general) and could use help with something possible very simple but I can't figure it out.</p>
  <p>Currently I am working on a Project Manager app in Django for my portfolio.</p>
  <p>But I am stuck with creating list of the different project and their tasks.</p>
  <p>I would like to create something like this:</p>
  <p>Project_1</p>
  <ol>
  <li>task item 1 (of project_1)</li>
  <li>task item 2 (of project_1)</li>
  </ol>
  <p>Project_2</p>
  <ol>
  <li>task item 1 (of project_2)</li>
  <li>task item 2 (of project_2)</li>
  </ol>
  <p>Project_3</p>
  <ol>
  <li>task item 1 (of project_3)</li>
  <li>task item 2 (of project_3)</li>
  <li>task item 3 (of project_3)</li>
  </ol>
  <p>Currently I have the following:</p>
  <p>view.py:</p>
  <pre><code>def project(request):
  &quot;&quot;&quot; Show all Projects&quot;&quot;&quot;
  projects = Project.objects.order_by('date_added')
  project = Project.objects.get(id=1)
  tasks = project.task_set.order_by('date_added')
  context = {'projects': projects, 'tasks': tasks}
  return render(request, 'tasks/project.html', context)
  </code></pre>
  <p>project.html</p>
  <pre><code>    {% extends &quot;tasks/base.html&quot; %}
  {% block content %}
  &lt;p&gt;Projects&lt;/p&gt;
  &lt;ul&gt;
      {% for project in projects %}
      &lt;li&gt;{{project}}&lt;/li&gt;
          &lt;ul&gt;
              {% for task in tasks %}
              &lt;li&gt;{{task}}&lt;/li&gt;
              {% empty %}
              &lt;li&gt;No tasks added &lt;/li&gt;
              {% endfor %}
          &lt;/ul&gt;
      {% empty %}
      &lt;li&gt;No projects have been added yet.&lt;/li&gt;
      {% endfor %}
  &lt;/ul&gt;
  
  {% endblock content %}
  </code></pre>
  <p>models.py</p>
  <pre><code>class Project(models.Model):
      &quot;&quot;&quot;A project the user is working on.&quot;&quot;&quot;
      text = models.CharField(max_length=200)
      date_added = models.DateTimeField(auto_now_add=True)
  
      def __str__(self):
          &quot;&quot;&quot;Return a string representation of the model.&quot;&quot;&quot;
          return self.text
  
  
  class Task(models.Model):
      &quot;&quot;&quot;A task of a project to complete.&quot;&quot;&quot;
      project = models.ForeignKey(Project, on_delete=models.CASCADE)
      text = models.TextField()
      date_added = models.DateTimeField(auto_now_add=True)
  
      class Meta:
          verbose_name_plural = 'tasks'
  
      def __str__(self):
          &quot;&quot;&quot;Return a string representation of the model.&quot;&quot;&quot;
          if(len(self.text) &lt; 50 ):
              return f&quot;{self.text}&quot;
          else:
              return f&quot;{self.text[:50]}...&quot;
  </code></pre>
  <p>The result I get is:</p>
  <p>Project_1</p>
  <ol>
  <li>task item 1 (of project_1)</li>
  <li>task item 2 (of project_1)</li>
  </ol>
  <p>Project_2</p>
  <ol>
  <li>task item 1 (of project_1)</li>
  <li>task item 2 (of project_1)</li>
  </ol>
  <p>Project_3</p>
  <ol>
  <li>task item 1 (of project_1)</li>
  <li>task item 2 (of project_1)</li>
  </ol>
  <p>I know the issue is that I use <code>project = Project.objects.get(id=1)</code> and because of this it only shows the tasks related to project one.
  And I think if the id is related to the right project id this might be the solution.
  I don't seem to find out how I do this. Or I might be looking in the wrong direction.</p>
  <p>Any help is welcome, thanks for in advance.</p>