如何從CVB DetailView中獲取get_context_data
中的'pk'或'id'?如何從CVB中的'get_context_data`中獲得'pk'或'id'?
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(r'^book/(?P<pk>\d+)/$', MyDetail.as_view(), name='book'),
在模板中使用象'something.0.pk' –
我糊塗由於CVB你是指基於類的視圖(CBV)?.. –