0

I'm new to Django and I can't find anywhere which instance variables do class-based generic views have. For example, I know that you can use self.request or self.kwargs, but is there any list of which other variables can I use? Maybe I can set any variable I need in the dispatch function, but what are the defaults?

1
  • 1
    One way to pass extra instance variables is to pass extra kwargs into as_view, see __init__ method of the View class. Commented Sep 13, 2013 at 19:14

1 Answer 1

2

I agree that this is not documented as well as it could be.

The overview of class-based generic views says this:

Various useful things are stored on self; as well as the request (self.request) this includes the positional (self.args) and name-based (self.kwargs) arguments captured according to the URLconf.

Then, the reference documentation lists the attributes created for each specific view. Under DetailView, for example, it says:

While this view is executing, self.object will contain the object that the view is operating upon.

Another reference you might want to consult is Classy Class-Based Views.

Sign up to request clarification or add additional context in comments.

2 Comments

I've never found a better way than just reading the code. The documentation is silent about enough aspects of class-based views that I don't trust it to be complete.
@Peter: Reading the code is certainly a good strategy. One nice thing about the ccbv site I mentioned is that each page links directly to the source code of the corresponding view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.