gpt4 book ai didi

Django - WSGIRequest'对象没有属性 'Get'

转载 作者:行者123 更新时间:2023-12-02 20:16:16 25 4
gpt4 key购买 nike

所以我有两种场景,一种有效,一种无效。我在 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'

最佳答案

这与您采用的方法无关。在第一种方法中,您编写:

if request.<b>Get</b>.get('Filter') in status_list:
# ...

正如错误所述,request 对象没有 Get 属性,只有 GET 属性。您在 View 的其余部分犯了同样的错误。

所以你可以用以下方法解决这个问题:

if request.<b>GET</b>.get('Filter') in status_list:
# ...

您还在过滤(两个 View )中编写了 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'
)

关于Django - WSGIRequest'对象没有属性 'Get',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52321149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com