Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the User Id Class Based View

How to get the Id of the User logged in a Class Based View ?

Here is my CBW

class IndexView(generic.ListView):
   template_name = 'polls/index.html'
   context_object_name = 'latest_question_list'
   context = 'activate'

   def get_context_data(self, **kwargs):
       context = super().get_context_data(**kwargs)  
       context['activate'] = 'Polls'
       return context

   def get_queryset(self):
       """Return the last five published questions."""
       return Question.objects.exclude(panel__user='???',  
                                       panel__valid=False)
                              .order_by('-pub_date')[:5]
like image 331
Bea Avatar asked Aug 01 '18 12:08

Bea


1 Answers

With self.request.user:

  def get_queryset(self):
       """Return the last five published questions."""
       return Question.objects.exclude(panel__user=self.request.user,  
                                       panel__valid=False)
                              .order_by('-pub_date')[:5]
like image 127
neverwalkaloner Avatar answered Sep 28 '22 09:09

neverwalkaloner