我的下面的代码产生错误全局名称'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')
答案 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/