Access request.user with Django class view

Hello. When using the View class for a view in Django like below.

Class CarView(View):
    def get(self, request):
          pass

To access the user object, in this instance in the get method, would I have to do self.request.user or request.user? Thanks.

Reader’s Digest version: In this specific case, it doesn’t matter.

In the context of this get method, request is the parameter being passed in to the function call. A reference to self.request would be a reference to a request object defined as an attribute of the current instance of the CarView object. Since you’re inheriting from the View class (assuming django.views.generic.base.View, the setup method in that view assigns the request to an instance attribute by that same name.

(Note: Other than for educational information, it seems ‘odd’ to me that anyone would want to directly inherit from View. In pretty much every practical case I can think of, if you’re using the generic views, there’s a better choice further down the hierarchy.)

Ken

1 Like

Ok thank you, this helps a lot.