因此,我有2个场景,其中一个起作用而一个不起作用。我在html模板中设置了一个选择框,其中包含一些在Scenrio2中可以看到的硬编码选项。 我很想知道为什么Scenario2可以正常工作,而Scenario1会引发此错误。我还附加了下面的堆栈跟踪。
场景1(无效):
current_status = 'All'
status_list = []
all_status = RequisitionStatus.objects.all()
for status in all_status:
status_list.append(status.status)
if request.Get.get('Filter') in status_list:
user_req_lines_incomplete = RequisitionLine.objects.filter(Q(parent_req__username=request.user) & Q(status__status=request.Get.get('Filter')))
current_status = request.Get.get('Filter')
else:
user_req_lines_incomplete = RequisitionLine.objects.filter(parent_req__username=request.user).exclude(status__status='Completed')
方案2(可行):
if request.GET.get('Filter') == 'Created':
user_req_lines_incomplete = RequisitionLine.objects.filter(Q(parent_req__username=request.user) & Q(status__status='Created'))
current_status = 'Created'
elif request.GET.get('Filter') == 'For Assistance':
user_req_lines_incomplete = RequisitionLine.objects.filter(Q(parent_req__username=request.user) & Q(status__status='For Assistance'))
current_status = 'For Assistance'
elif request.GET.get('Filter') == 'Assistance Complete':
user_req_lines_incomplete = RequisitionLine.objects.filter(Q(parent_req__username=request.user) & Q(status__status='Assistance Complete'))
current_status = 'Assistance Complete'
elif request.GET.get('Filter') == 'Assistance Rejected':
user_req_lines_incomplete = RequisitionLine.objects.filter(Q(parent_req__username=request.user) & Q(status__status='Assistance Rejected'))
current_status = 'Assistance Rejected'
else:
user_req_lines_incomplete = RequisitionLine.objects.filter(parent_req__username=request.user).exclude(status__status='Completed')
Traceback:
File "C:\Users\Kevin.Pardo\Documents\Python\venv\env\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Users\Kevin.Pardo\Documents\Python\venv\env\lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "C:\Users\Kevin.Pardo\Documents\Python\venv\env\lib\site-packages\django\core\handlers\base.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Kevin.Pardo\Documents\Python\venv\env\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
21. return view_func(request, *args, **kwargs)
File "C:\Users\Kevin.Pardo\Documents\Python\django\lambda\req\views.py" in pending_action
241. if request.Get.get('Filter') in status_list:
Exception Type: AttributeError at /req/pending/
Exception Value: 'WSGIRequest' object has no attribute 'Get'
答案 0 :(得分:4)
与您遵循的方法无关。在您的第一种方法中,您编写:
if request.Get.get('Filter') in status_list:
# ...
就像错误提示一样,request
对象没有Get
属性,只有GET
属性。您在其余视图中也会犯同样的错误。
因此您可以使用以下方法解决此问题:
if request.GET.get('Filter') in status_list:
# ...
您还(在两个视图中)在过滤中都写了parent_req__username=request.user
,我认为最好使用parent_req=request.user
或parent_req__username=request.user.username
)。
实际上,您可以进一步改善这一点,例如:
current_status = 'All'
all_status = RequisitionStatus.objects.values_list('status', flat=True)
filter = request.GET.get('filter')
if filter in status_list:
user_req_lines_incomplete = RequisitionLine.objects.filter(
parent_req=request.user,
status__status=filter
)
current_status = filter
else:
user_req_lines_incomplete = RequisitionLine.objects.filter(
parent_req=request.user
).exclude(
status__status='Completed'
)