如何从CVB中获得'pk'或'id' in ' get_context_data ' ?

时间:2022-08-17 19:22:30

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

如何从CVB DetailView中获取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:

url:

url(r'^book/(?P<pk>\d+)/$', MyDetail.as_view(), name='book'),

5 个解决方案

#1


19  

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

你可以从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.

我不知道为什么要这样做,因为超类已经获得了与pk对应的书——这就是DetailView的全部意义。

#2


4  

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=self.kwargs.get('pk'))
            return context

#3


3  

In get_context_data you already have the object in self.object (and you can do self.object.pk). Here's what happens upstream in the class hierarchy (DetailView inherits from BaseDetailView):

在get_context_data中,对象已经是self。对象(你可以做self。object.pk)。下面是类层次结构上游发生的情况(DetailView继承自BaseDetailView):

class BaseDetailView(SingleObjectMixin, View):
"""
A base view for displaying a single object
"""
def get(self, request, *args, **kwargs):
    self.object = self.get_object()
    context = self.get_context_data(object=self.object)
    return self.render_to_response(context)

Reading Django source code to understand stuff is incredibly easy.

阅读Django源代码来理解这些内容非常容易。

And by the way, I am not sure you can always rely on the fact that kwargs has a 'pk' key.

顺便说一下,我不确定你是否能一直相信kwargs有一个pk键。

#4


2  

In addition to getting it from self.kwargs as Daniel Roseman suggested, you can use self.get_object().pk, for example if you change your URL identifier from pk to, say, slug or something.

除了从自我中得到它。正如Daniel Roseman建议的kwargs,您可以使用self.get_object()。例如,如果你把你的URL标识符从pk改为蛞蝓或别的什么。

#5


0  

you can simply get it in the 'get' method, like this:

你可以通过“get”方法得到它,比如:

def get_context_data(self, request, pk, *args, **kwargs):
    context = super(MyDetail, self).get_context_data(**kwargs)
    context['something'] =Book.objects.filter(pk=self.kwargs.get('pk'))
    return context

#1


19  

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

你可以从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.

我不知道为什么要这样做,因为超类已经获得了与pk对应的书——这就是DetailView的全部意义。

#2


4  

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=self.kwargs.get('pk'))
            return context

#3


3  

In get_context_data you already have the object in self.object (and you can do self.object.pk). Here's what happens upstream in the class hierarchy (DetailView inherits from BaseDetailView):

在get_context_data中,对象已经是self。对象(你可以做self。object.pk)。下面是类层次结构上游发生的情况(DetailView继承自BaseDetailView):

class BaseDetailView(SingleObjectMixin, View):
"""
A base view for displaying a single object
"""
def get(self, request, *args, **kwargs):
    self.object = self.get_object()
    context = self.get_context_data(object=self.object)
    return self.render_to_response(context)

Reading Django source code to understand stuff is incredibly easy.

阅读Django源代码来理解这些内容非常容易。

And by the way, I am not sure you can always rely on the fact that kwargs has a 'pk' key.

顺便说一下,我不确定你是否能一直相信kwargs有一个pk键。

#4


2  

In addition to getting it from self.kwargs as Daniel Roseman suggested, you can use self.get_object().pk, for example if you change your URL identifier from pk to, say, slug or something.

除了从自我中得到它。正如Daniel Roseman建议的kwargs,您可以使用self.get_object()。例如,如果你把你的URL标识符从pk改为蛞蝓或别的什么。

#5


0  

you can simply get it in the 'get' method, like this:

你可以通过“get”方法得到它,比如:

def get_context_data(self, request, pk, *args, **kwargs):
    context = super(MyDetail, self).get_context_data(**kwargs)
    context['something'] =Book.objects.filter(pk=self.kwargs.get('pk'))
    return context