Bootstrap4
Bootstrap4

Reputation: 3841

What is the context variable for detailview class in Django?

Django's generic view class ListView has object_list as context variable, what is the context variable for the DetailView?

My view is,

class MyDetail(DetailView):
   model = Mymodel

Upvotes: 1

Views: 1068

Answers (2)

hacker_man
hacker_man

Reputation: 1

You can change the context object name by using :

context_object_name = 'your_object_name'

Include this in the class in your View. By default, the object name is 'object' , but it is always good to use an object name of your choice. It makes it more intuitive!

Upvotes: 0

Eugene Yarmash
Eugene Yarmash

Reputation: 150188

The context data for a DetailView class contains the "object" key, which points to the object that the view is operating on.

You can use a different key if you override the get_context_data() method of the view, e.g.:

 def get_context_data(self, **kwargs):
     context = super().get_context_data(**kwargs)
     context["my_object"] = self.object
     return context

Upvotes: 3

Related Questions