Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get 'pk' or 'id' in `get_context_data` from CBV?

How can I get 'pk' or 'id' in get_context_data from CBV DetailView?

class MyDetail(DetailView):     model = Book     template_name = 'book.html'          def get_context_data(self, **kwargs):             context = super(MyDetail, self).get_context_data(**kwargs)             context['something'] = Book.objects.filter(pk=pk)             return context 

url:

url(r'^book/(?P<pk>\d+)/$', MyDetail.as_view(), name='book'), 
like image 417
user3284589 Avatar asked Feb 13 '14 15:02

user3284589


Video Answer


1 Answers

You can get it from self.kwargs['pk'].

I'm not sure why you want to, though, since the superclass already gets the Book corresponding to that pk - that's the whole point of a DetailView.

like image 157
Daniel Roseman Avatar answered Sep 24 '22 21:09

Daniel Roseman