NoReverseMatch发生在任何异常中

时间:2018-08-27 03:49:54

标签: django django-rest-framework

我正在使用django-framework。 有一个问题是,程序中发生任何异常都会调用NoReverseMatch(例如,我在视图代码中输入了“ a = 1/0”)。

以下是我的追溯:

  File "/usr/lib/python3.5/wsgiref/handlers.py", line 137, in run
    self.result = application(self.environ, self.start_response)
  File "/usr/local/lib/python3.5/dist-packages/django/contrib/staticfiles/handlers.py", line 66, in __call__
    return self.application(environ, start_response)
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/wsgi.py", line 146, in __call__
    response = self.get_response(request)
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 81, in get_response
    response = self._middleware_chain(request)
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py", line 37, in inner
    response = response_for_exception(request, exc)
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py", line 87, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py", line 122, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "/usr/local/lib/python3.5/dist-packages/django/views/debug.py", line 91, in technical_500_response
    text = reporter.get_traceback_text()
  File "/usr/local/lib/python3.5/dist-packages/django/views/debug.py", line 341, in get_traceback_text
    return t.render(c)
  File "/usr/local/lib/python3.5/dist-packages/django/template/base.py", line 175, in render
    return self._render(context)
  File "/usr/local/lib/python3.5/dist-packages/django/template/base.py", line 167, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python3.5/dist-packages/django/template/base.py", line 943, in render
    bit = node.render_annotated(context)
  File "/usr/local/lib/python3.5/dist-packages/django/template/base.py", line 910, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.5/dist-packages/django/template/defaulttags.py", line 211, in render
    nodelist.append(node.render_annotated(context))
  File "/usr/local/lib/python3.5/dist-packages/django/template/base.py", line 910, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.5/dist-packages/django/template/base.py", line 993, in render
    output = self.filter_expression.resolve(context)
  File "/usr/local/lib/python3.5/dist-packages/django/template/base.py", line 703, in resolve
    new_obj = func(obj, *arg_vals)
  File "/usr/local/lib/python3.5/dist-packages/django/template/defaultfilters.py", line 229, in stringformat
    return ("%" + str(arg)) % value
  File "/usr/local/lib/python3.5/dist-packages/django/utils/functional.py", line 79, in __repr__
    return repr(self.__cast())
  File "/usr/local/lib/python3.5/dist-packages/django/utils/functional.py", line 124, in __cast
    return self.__text_cast()
  File "/usr/local/lib/python3.5/dist-packages/django/utils/functional.py", line 112, in __text_cast
    return func(*self.__args, **self.__kw)
  File "/usr/local/lib/python3.5/dist-packages/django/urls/base.py", line 86, in reverse
    raise NoReverseMatch("%s is not a registered namespace" % key)
django.urls.exceptions.NoReverseMatch: 'promotions' is not a registered namespace
[2018-08-27 11:37:56,758] [ERROR] "POST /v1/voucher/ HTTP/1.1" 500 59

注意:回溯将显示服务时间,真正的错误将在它们之间打印。

下面是我的urls.config

router = DefaultRouter()
router.register('address', AddressViewset, base_name="address")
router.register('category', CategoryViewset, base_name='Category')
router.register('product', ProductViewset, base_name='product')
router.register('history', UserProductViewset, base_name='browsehistory')
router.register('order', OrderViewset, base_name='order')
router.register('voucher', VoucherViewset, base_name='voucher')

urlpatterns = [

    path('admin/', admin.site.urls),
    url('v1/', include(router.urls)),
    url('v1/login/', include('login.urls')),

]

以下是我的INSTALLED_APPS:

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'django.contrib.flatpages',
'rest_framework',
'rest_framework.authtoken',
'django_filters',
'compressor',
'widget_tweaks',
'login.apps.LoginConfig',
] + get_core_apps(['catalogue', 'address', 'voucher', 'order', 'analytics', 
'offer', 'wishlists', 'dashboard'])

get_core_apps是django-oscar功能。

我继续深入研究,并在django / urls / base.py第75行中使用pdb,错误从哪里开始。

下面是代码段:

    import pdb
    pdb.set_trace()
    extra, resolver = resolver.namespace_dict[ns]
    resolved_path.append(ns)
    ns_pattern = ns_pattern + extra
    ns_converters.update(resolver.pattern.converters)

然后我打印一些变量

  • ns:“促销活动”
  • resolver.namespace_dict:{'admin':('admin \ /',(admin:admin)'admin /'>)}

总而言之,当发生错误时,django希望找到URL“ home:promotions”的视图函数,但是找不到,我已经检查了所有发生“ home:promotions”的地方(在django-oscar中显示代码,但不是我的代码),程序也没有碰到它们。我认为最重要的是找出哪一行为“ home:prmotions”调用reverse(),如何进行调试。

1 个答案:

答案 0 :(得分:1)

我找到原因了。 首先,我将pdb放在url / base.py中,并得到错误视图的名称为“ promotion:home”。然后在我的所有项目和django-oscar中搜索它,将其分配给全局变量:

OSCAR_HOMEPAGE = reverse_lazy('promotions:home')

当我对此发表评论时,错误消失了