Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Internal Server Error instead of 404

I am using Django 1.6, uwsgi and nginx, the application works fine but I am getting 500 error and the email below for every invalid URL that I am trying to access, instead of a 404 error.

I get this for http://my_project_url.com/whatever or even for http://my_project_url.com/favicon.ico

I have looked over the URL's but there is no regex matching this pattern.

Here is the traceback from the email:

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_

If I am trying to access an URL from the app where I do raise Http404 it's fine, I get the regular nginx error page.

like image 209
cornelv Avatar asked Feb 13 '23 18:02

cornelv


1 Answers

It seems you have a custom processor trying to resolve path:

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

Quoting django resolve() docs:

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

I suggest to you to manage exception in custom processor code to look like this one:

try:
    resolve_result = resolve(request.path)
    app_name = resolve_result.app_name
    ... your code ....
except Resolver404:
    pass
like image 114
dani herrera Avatar answered Feb 15 '23 09:02

dani herrera