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应包含用户名
答案 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")