[Answered ]-How to access pk in views (Django)

2👍

You should define the get_queryset method instead of the standalone queryset attribute.

def get_queryset(self, *args, **kwargs):
    return Comment.objects.filter(chat_id=self.kwargs['pk'])

0👍

Instead of CommentList you can use plain view:

def comments_index(request, chatid):
    return render(request, 'xxx/comment_list.html', {
        'comments': Comment.objects.filter(chat_id=chatid)
    })

And in urls:

url(r'^comments/(?P<chatid>[0-9]+)/$', views.comments_index, name='comments'),

Leave a comment