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

时间:2014-06-12 11:08:07

标签: python django nginx uwsgi

我正在使用Django 1.6,uwsgi和nginx,该应用程序运行正常,但我收到500错误,以及我尝试访问的每个无效网址的下面的电子邮件,而不是404错误。

我的http://my_project_url.com/whatever甚至是http://my_project_url.com/favicon.ico

我查看了网址,但没有匹配此模式的正则表达式。

以下是电子邮件的追溯:

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_

如果我尝试从应用中访问我raise Http404的网址,那么我会收到常规的nginx错误页面。

1 个答案:

答案 0 :(得分:7)

似乎您有自定义处理器尝试解析路径

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

引用django resolve()文档:

  

如果URL未解析,该函数会引发Resolver404异常(Http404的子类)。

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

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