user = request.user - 未定义名称'request'

时间:2014-02-24 08:46:26

标签: python django

from django.contrib.auth.models import User
...
class FruitList(ListView):
    model = Fruit
    paginate_by = "2"
    queryset = Fruit.objects.filter(user=request.user)
    context_object_name = "myfruitlist"
    template_name = 'myfruit_list.html'

错误:

  

名称'request'未定义

我该如何解决?

1 个答案:

答案 0 :(得分:3)

您必须以这种方式访问​​它:

def get_queryset(self):
    return Fruit.objects.filter(user=self.request.user)

您可以阅读有关此here in docs

的信息