python – Django内部服务器错误而不是404

我正在使用Django 1.6,uwsgi和nginx,该应用程序工作正常但我收到500错误和下面的电子邮件我想要访问的每个无效URL,而不是404错误.

我得到这个http://my_project_url.com/whatever甚至http://my_project_url.com/favicon.ico

我查看了URL,但没有匹配此模式的正则表达式.

这是电子邮件中的回溯:

Traceback (most recent call last):

  File "/project/virtenv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 152, in get_response
    response = callback(request, **param_dict)

  File "/project/virtenv/local/lib/python2.7/site-packages/django/utils/decorators.py", line 99, in _wrapped_view
    response = view_func(request, *args, **kwargs)

  File "/project/virtenv/local/lib/python2.7/site-packages/django/views/defaults.py", line 30, in page_not_found
    body = template.render(RequestContext(request, {'request_path': request.path}))

  File "/project/virtenv/local/lib/python2.7/site-packages/debug_toolbar/panels/templates/panel.py", line 55, in _request_context__init__
    context = processor(request)

  File "./project/context_processors.py", line 88, in app_delegate
    app_name = resolve(request.path).app_name

  File "/project/virtenv/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 453, in resolve
    return get_resolver(urlconf).resolve(path)

  File "/project/virtenv/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 333, in resolve
    raise Resolver404({'tried': tried, 'path': new_path})

Resolver404: {u'path': u'favicon.ico', u'tried': [[<RegexURLResolver <module 'custom_

如果我试图从应用程序访问我提升Http404的URL,那很好,我得到常规的nginx错误页面.

最佳答案
看来你有一个自定义处理器试图解决路径:

File "./project/context_processors.py", line 88, in app_delegate
  app_name = resolve(request.path).app_name

引用django resolve()文档:

If the URL does not resolve, the function raises a Resolver404 exception (a subclass of Http404) .

我建议您在自定义处理器代码中管理异常,看起来像这样:

try:
    resolve_result = resolve(request.path)
    app_name = resolve_result.app_name
    ... your code ....
except Resolver404:
    pass

转载注明原文:python – Django内部服务器错误而不是404 - 代码日志