如何从 CBV 获取 `get_context_data` 中的 'id'?

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

我有点困惑,因为我必须重写 get_context_data 方法才能使用模板上下文。

urls.py

re_path(
    r"^post/(?P<id>\d+)/$",PostView.as_view(template_name="pages/post.html"),
    name="post",
),

views.py

class PostView(TemplateView):
    template_name = 'djangoapp/pages/post.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['dataC'] = get_object_or_404(Content, Título_id="2")
        return context

您可以从self.kwargs['pk'].

获取