Error 500 instead of 404

80 views
Skip to first unread message

Ricardo F. Teixeira

unread,
Nov 20, 2011, 5:56:23 AM11/20/11
to Django users
Hello everybody!

I'm having a strange error.
I already searched for this error and there's quite a lot of
discussion about it. But I didn't find a proper answer that fits me.
So... when I set DEBUG to True and access an invalid URL Django
returns me a 404 error page, but if I set DEBUG to False it gives me
an 500...

Firstly I thought it was something wrong with my project so I started
a new one, with only the following basics options.
I can assure you that I only edit those 3 files (views.py, urls.py and
settings.py).

In settings.py I only change from DEGUG=True to DEBUG=False.

ricardo@maggie:~/ola $ ls
__init__.py __init__.pyc manage.py settings.py settings.pyc
urls.py urls.pyc views.py views.pyc

ricardo@maggie:~/ola $ cat urls.py
from django.conf.urls.defaults import patterns, include, url

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = patterns('',
# Examples:
url(r'^$', 'ola.views.home', name='home'),
# url(r'^ola/', include('ola.foo.urls')),

# Uncomment the admin/doc line below to enable admin
documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
)
ricardo@maggie:~/ola $ cat views.py
from django.http import HttpResponseNotFound

def home(request):
return HttpResponseNotFound()

ricardo@maggie:~/ola $ python manage.py runserver
Validating models...

0 errors found
Django version 1.3.1, using settings 'ola.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[19/Nov/2011 23:53:21] "GET / HTTP/1.1" 404 0
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/django/core/servers/
basehttp.py", line 283, in run
self.result = application(self.environ, self.start_response)
File "/Library/Python/2.7/site-packages/django/core/handlers/
wsgi.py", line 272, in __call__
response = self.get_response(request)
File "/Library/Python/2.7/site-packages/django/core/handlers/
base.py", line 153, in get_response
response = self.handle_uncaught_exception(request, resolver,
sys.exc_info())
File "/Library/Python/2.7/site-packages/django/core/handlers/
base.py", line 218, in handle_uncaught_exception
return callback(request, **param_dict)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py",
line 93, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/views/defaults.py",
line 30, in server_error
t = loader.get_template(template_name) # You need to create a
500.html template.
File "/Library/Python/2.7/site-packages/django/template/loader.py",
line 157, in get_template
template, origin = find_template(template_name)
File "/Library/Python/2.7/site-packages/django/template/loader.py",
line 138, in find_template
raise TemplateDoesNotExist(name)
TemplateDoesNotExist: 500.html

[19/Nov/2011 23:53:48] "GET /GIVE_ME_400 HTTP/1.1" 500 1314
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/django/core/servers/
basehttp.py", line 283, in run
self.result = application(self.environ, self.start_response)
File "/Library/Python/2.7/site-packages/django/core/handlers/
wsgi.py", line 272, in __call__
response = self.get_response(request)
File "/Library/Python/2.7/site-packages/django/core/handlers/
base.py", line 153, in get_response
response = self.handle_uncaught_exception(request, resolver,
sys.exc_info())
File "/Library/Python/2.7/site-packages/django/core/handlers/
base.py", line 218, in handle_uncaught_exception
return callback(request, **param_dict)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py",
line 93, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/views/defaults.py",
line 30, in server_error
t = loader.get_template(template_name) # You need to create a
500.html template.
File "/Library/Python/2.7/site-packages/django/template/loader.py",
line 157, in get_template
template, origin = find_template(template_name)
File "/Library/Python/2.7/site-packages/django/template/loader.py",
line 138, in find_template
raise TemplateDoesNotExist(name)
TemplateDoesNotExist: 500.html

Thank you for your time.

Best regards,

Ricardo F. Teixeira

Nick Apostolakis

unread,
Nov 20, 2011, 9:24:53 AM11/20/11
to django...@googlegroups.com
If the template exists, then
it could easily be a directory or file permission error.
I have seen the same in my installation a number of times :)
Check out the paths and the permissions

--
--------------------------------------------------------------
Nick Apostolakis
e-mail: nick...@oncrete.gr
Web Site: http://nick.oncrete.gr
--------------------------------------------------------------


Tomek Paczkowski

unread,
Nov 20, 2011, 9:28:04 AM11/20/11
to django...@googlegroups.com
If you dont have 404.html file, than Django will raise exception that 404.html is not found. Next, if you dont have 500.html your exception is generating new exception on lack of 500.html. Make sure you have both files. Eg:

$ touch templates/{404,500}.html

Ricardo F. Teixeira

unread,
Nov 20, 2011, 3:00:56 PM11/20/11
to Django users
Solved (!) by creating those template files.
Thanks Tomek!
Reply all
Reply to author
Forward
0 new messages