[Fixed]-Django pages and access to parent object

1๐Ÿ‘

โœ…

You can add whatever data you like into a view by defining get_context_data().

def get_context_data(self, *args, **kwargs):
    context = super(ListView, self).get_context_data(*args, **kwargs):
    context['question'] = Question.objects.get(pk=self.kwargs['pk'])
    return context

Alternatively, you might reflect that your original DetailView already gives you access to both the question โ€“ via the default object โ€“ and its answers โ€“ via {{ object.answer_set.all }} in the template, without having to do anything else. It seems unlikely that a question would have enough answers to need paginating, which is the main benefit that the ListView gives you.

๐Ÿ‘คDaniel Roseman

Leave a comment