全局名称'request'未定义错误而不是它转到模板

时间:2016-05-02 14:01:38

标签: django

我的下面的代码产生错误全局名称'request'未定义。我正在使用django。我究竟做错了什么?如果此人从未订购过,我希望它转到模板np_orders.html。

class OrderList(LoginRequiredMixin, ListView):
queryset = Order.objects.all()

def get_queryset(self):
    try:
        user_check_email = self.request.user.email
        user_checkout = UserCheckout.objects.get(email=user_check_email)
        return super(OrderList, self).get_queryset().filter(user=user_checkout)
    except ObjectDoesNotExist:
        return render(request, 'no_orders.html')

1 个答案:

答案 0 :(得分:0)

您似乎没有将request作为参数添加到视图功能中。

class OrderList(LoginRequiredMixin, ListView):
queryset = Order.objects.all()

def get_queryset(self,request):
    try:
        user_check_email = self.request.user.email
        user_checkout = UserCheckout.objects.get(email=user_check_email)
        return super(OrderList, self).get_queryset().filter(user=user_checkout)
    except ObjectDoesNotExist:
        return render(request, 'no_orders.html')

请查看此文档。

https://docs.djangoproject.com/es/1.9/topics/class-based-views/intro/