Get request object in class mode

I want the current user to log in based on a class. I can do this by retrieving the user from the request object, but how can I get this object?

class HomeView(TemplateView): template_name='home.html' def get_context_data(self, **kwargs): context = super(HomeView, self).get_context_data(**kwargs) context['username'] = request.user.username return context 
+7
django
source share
1 answer

You can access it with self.request.user

For example, you can do this in your CBV

 if self.request.user.is_authenticated(): ... 

or

 context['username'] = self.request.user.username ... 

etc.

+14
source share

All Articles