Flupper
Flupper

Reputation: 321

get_context_data function in the Detailview even didn't call but still could call the context

I have commented out the get_context_data function (below), but in the template, I can still call {{ object }}, could anyone please explain how this is possible?

# def get_context_data(self, *args,**kwargs):
#     context=super(productdetailview,self).get_context_data(*args,**kwargs)
#     print(context)
#     print(self.kwargs.get("pk"))
#     return context

Here the whole code

class ProductDetailSlugView(DetailView):
queryset = product.objects.all()
template_name = "product/detail.html"
print(queryset)

# def get_context_data(self, *args,**kwargs):
#     context=super(productdetailview,self).get_context_data(*args,**kwargs)
#     print(context)
#     print(self.kwargs.get("pk"))
#     return context

# def get_context_data(self, *args, **kwargs):
#     context=super(ProductDetailSlugView,self).get_context_data(*args,**kwargs)
#     context['cart']=cart.objects.get_or_create(self.request)
#     # print(context)
#     return context

# def get_object(self, *args,**kwargs):
#     print(args)
#     print(kwargs)
#     pk=self.kwargs.get("pk")
#     slug=self.kwargs.get("slug")
#     print(self.kwargs.get("slug"))
#     print(product.objects.get_by_slug(slug))
#     return product.objects.get_by_slug(slug)

Upvotes: 0

Views: 392

Answers (1)

Alexey
Alexey

Reputation: 1438

DetailView inherits from BaseDetailView which inherits from SingleObjectMixin where get_context_data is called

Upvotes: 2

Related Questions