[Fixed]-Django DetailView get_context_data

28👍

Project is the model class, so doing (list=Project) doesn’t make sense.

If you want to access the object in the detail view’s get_context_data method, you can use self.object:

def get_context_data(self, **kwargs):
    context = super(ProjectDetail, self).get_context_data(**kwargs)
    context['tasks'] = Task.objects.filter(list=self.object)
    return context

However, you don’t actually have to override the get_context_data method at all. In your template, you can follow the relationship backwards from a project to get its tasks:

{% for task in object.task_set.all %}
  <li>{{task}}</li>
{% endfor %}

Leave a comment