django 如何修复此错误AttributeError:“NoneType”对象没有属性“extraBottles”?

3okqufwl  于 2023-05-30  发布在  Go
关注(0)|答案(1)|浏览(221)

我的代码有问题。返回"AttributeError: 'NoneType' object has no attribute 'extraBottles'"的错误日志

Internal Server Error: /employee/order/79/
Traceback (most recent call last):
  File "/mnt/c/Users/davep/Desktop/a/Water_management/newenv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/mnt/c/Users/davep/Desktop/a/Water_management/newenv/lib/python3.10/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/mnt/c/Users/davep/Desktop/a/Water_management/newenv/lib/python3.10/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/mnt/c/Users/davep/Desktop/a/Water_management/employee/views.py", line 44, in view_order
    day_.extraBottles += request.session['extraBottles']
AttributeError: 'NoneType' object has no attribute 'extraBottles'
mspsb9vt

mspsb9vt1#

您的day_ object为None,因此当您尝试访问extraBottles属性时,它会抛出错误。在尝试访问day对象的属性或使用getattr函数之前,应该检查day对象是否不是None。

if day is not None:
    day_.extraBottles += request.session['extraBottles']

相关问题