如何使当前用户拥有Django基于类的视图

时间:2019-04-15 09:00:57

标签: django

NameError: name 'request' is not defined

class PostListViewPrv(ListView):
    queryset = Post.objects.filter(wszyscy=False, pracownik=request.user, published_date__lte=timezone.now()).order_by('-published_date')
    context_object_name = 'posts'
    paginate_by = 2
    template_name = 'komunikaty/komunikatyPrv.html'

变量pracownik应包含用户名

1 个答案:

答案 0 :(得分:3)

您将必须覆盖get_queryset()

class PostListViewPrv(ListView):
    queryset = Post.objects.all()
    context_object_name = "posts"
    paginate_by = 2
    template_name = "komunikaty/komunikatyPrv.html"

    def get_queryset(self):
        return self.queryset.filter(
            wszyscy=False,
            pracownik=self.request.user,
            published_date__lte=timezone.now(),
        ).order_by("-published_date")