Override get_queryset DetailView

I'm new in Django and i try to use Class Based Views for my project, but i have problem in this. I wanna login a user into a page with its primary key but i don't know how to override query_set. here is my code, any help will grateful.

in the views.py :

class UserPage(LoginRequiredMixin, DetailView):
   template_name = 'user_page.html'
   model = User
   login_url = '/login/'

   def get_queryset(self):

in urls.py:

path('user/<int:pk>/' , UserPage.as_view()),

I write this :

return User.objects.get(pk=pk)

but i got this error :

Type Error missing 1 required positional argument: 'pk'

Start with this: class based views are supposed to be classes, so make a change:

# before
def UserPage(LoginRequiredMixin, DetailView):

# after
class UserPage(LoginRequiredMixin, DetailView):

Why would you override get_queryset anyway? Django will find that object for you. You are not suppose to do that in DetailView (it might be even not possible, but I'm not sure). If you want to add something, then add it in context:

class UserPage(...):
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['all_users'] = User.objects.all()
        return context
Back to Top