如何修复此错误 AttributeError: ‘NoneType’ 对象没有属性 ‘extraBottles’?

huangapple go评论54阅读模式
英文:

How to fix this error AttributeError: 'NoneType' object has no attribute 'extraBottles'?

问题

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

内部服务器错误:/employee/order/79/
追踪回溯(最近的调用在最上面):
  文件"/mnt/c/Users/davep/Desktop/a/Water_management/newenv/lib/python3.10/site-packages/django/core/handlers/exception.py"中的第34行,函数inner:
    response = get_response(request)
  文件"/mnt/c/Users/davep/Desktop/a/Water_management/newenv/lib/python3.10/site-packages/django/core/handlers/base.py"中的第115行,函数_get_response:
    response = self.process_exception_by_middleware(e, request)
  文件"/mnt/c/Users/davep/Desktop/a/Water_management/newenv/lib/python3.10/site-packages/django/core/handlers/base.py"中的第113行,函数_get_response:
    response = wrapped_callback(request,*callback_args,**callback_kwargs)
  文件"/mnt/c/Users/davep/Desktop/a/Water_management/employee/views.py"中的第44行,函数view_order:
    day_.extraBottles += request.session['extraBottles']
AttributeError: 'NoneType'对象没有属性'extraBottles'
英文:

I am having a problem with my code. It returns error log of "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'

答案1

得分: 0

你的 day_ 对象是 None,所以当你尝试访问 extraBottles 属性时会抛出错误。在尝试访问它的属性之前,你应该检查一下 day 对象是否不是 None,或者使用 getattr 函数。

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

Your day_ object is None so it throws an error when you try to access the extraBottles attribute. You should check if the day object is not None before trying to access it's attribute or use getattr function.

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

huangapple
  • 本文由 发表于 2023年5月28日 16:46:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76350659.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定