ObjectDoesNotExist没有捕获到DoesNotExist异常

时间:2017-11-09 17:38:51

标签: django authentication

我正在尝试捕获DoesNotExist异常,并且检查抛出异常的代码很困难。我无法到达在pdb中抛出它的行,并且在调试跟踪中没有关于什么对象抛出异常的信息,因为它包含在实例变量中,因此django跟踪不显示它的值。

我已在https://docs.djangoproject.com/en/1.11/ref/exceptions/#objectdoesnotexist阅读了文档,并在Catching Any DoesNotExist Error阅读了此问题。这两个都表明要捕获任何DoesNotExist异常,只需导入ObjectDoesNotExist并覆盖所有这些异常。这不是我的情况。除了ObjectDoesNotExist块之外没有捕获异常。

代码:

from django.core.exceptions import ObjectDoesNotExist

def check_permission(user):
    """function checks whether the user is in the list of allowed groups"""
    for option in ALLOWED: # list of groups, constant
        try:
            if user.groups.get().name == option:
                return True
        except ObjectDoesNotExist:
            pass
        else:
            pass
    return False

跟踪内容如下:

File 
".../lib/python3.6/site-packages/django/db/models/query.py", line 379, in get self.model._meta.object_name

django.contrib.auth.models.DoesNotExist: Group matching query does not exist.

我的代码中的行被抛出:

if user.groups.get().name == option

尝试访问django.contrib.auth.models.DoesNotExist只返回

AttributeError: model 'django.contrib.auth.models' has no attribute 'DoesNotExist'

我怎么抓住这个?有时候用户不会有任何群组,即使网站的设计是为了永远不会发生,但这仍然让我感到紧张,因为这里有可破解的代码。

编辑:python 3.6.2 django 1.11

完整追踪:

Traceback (most recent call last):
  File ".../lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
  File ".../lib/python3.6/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
response = self._get_response(request)
  File ".../lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
  File ".../lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File ".../lib/python3.6/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
return view_func(request, *args, **kwargs)
  File ".../lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
  File ".../lib/python3.6/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
  File "/Users/Mark/Desktop/Professional/FSC/water/WATER/sharedClasses.py", line 116, in get
return self.permission_denied_page(request, plans)
  File "/Users/Mark/Desktop/Professional/FSC/water/WATER/sharedClasses.py", line 68, in permission_denied_page
their_acccount = user.groups.get().name
  File ".../lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
  File ".../lib/python3.6/site-packages/django/db/models/query.py", line 379, in get
self.model._meta.object_name
django.contrib.auth.models.DoesNotExist: Group matching query does not exist.

1 个答案:

答案 0 :(得分:4)

您应该导入Group并捕获如下:'

from django.contrib.auth.models import Group

try:
    // your code
except Group.DoesNotExist:
   // to handle
顺便说一下,你为什么不以非常优化的方式写下你的方法:

def check_permission(user):
   """return true if the user is in options, false if not"""
   if user.groups.filter(name__in=ALLOWED).exists():
      return True
   else:
      return False