500 displayed instead of 404

1,610 views
Skip to first unread message

janedenone

unread,
Oct 28, 2008, 10:36:15 AM10/28/08
to Django users
Hi,

I use the following simple view

def index(request, page_id, current_subpage=1):
try:
current_page = get_object_or_404(Page, pk=page_id)
except:
# if anything else goes wrong, display the 404 anway
raise Http404

In debug mode, my app returns the detailed 'page not found' page for
non-existing pages, but as soon as I switch debug to false, the
500 template is shown. I can, however, display render and return the
404 template manually.

What could possibly go wrong when a Http404 exception is raised, i.e.
why does Django use the 500 template in this case?

Kind regards,
Jan


Ronaldo Zacarias Afonso

unread,
Oct 28, 2008, 10:59:50 AM10/28/08
to django...@googlegroups.com
Hi Jane,

Are you sure your index view is being executed?
It just seems that django can't find a URLconf rule defined for your view.

[]s
Ronaldo.

Steve Holden

unread,
Oct 28, 2008, 11:32:51 AM10/28/08
to django...@googlegroups.com
That depends whether your site does anything special with 404 errors.
Some sites display them with full decoration, and so there's always the
chance that 404 processing goes squiffy, in which case you may well see
a 500.

It's clear that some code is being executed in processing the 404
without debug that *isn't* executed when you process a 404 with debug
set. Now you just have to find out what ...

regards
Steve

Thomas Guettler

unread,
Oct 28, 2008, 11:33:23 AM10/28/08
to django...@googlegroups.com
Hi,

I guess that you get 500 because there is an unhandled exception. Can
you see
a traceback in your error log?

What happens if you write "return django.http.HttpResponse('OK')"
instead of raise Http404?

Thomas

janedenone schrieb:


--
Thomas Guettler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de

janedenone

unread,
Oct 28, 2008, 2:58:41 PM10/28/08
to Django users
Hi,

it must be some kind of unhandled exception, but I fail to see where
it might occur. I now boiled down the app to a single URL pattern and
a single view:

# urls.py:
urlpatterns = patterns('myproject.myapp.views',
(r'^test/$', 'test'),
)

# views.py:
def test(request):
raise Http404

When I rename the template 500.html to something else, this is
printed:

...

File "/Library/Python/2.5/site-packages/django/core/handlers/base.py",
line 116, in get_response
return self.handle_uncaught_exception(request, resolver,
sys.exc_info())

File "/Library/Python/2.5/site-packages/django/core/handlers/
base.py", line 158, in handle_uncaught_exception
return callback(request, **param_dict)

File "/Library/Python/2.5/site-packages/django/views/defaults.py",
line 88, in server_error
t = loader.get_template(template_name) # You need to create a
500.html template.

...

So there is indeed an uncaught exception which makes Django use the
500.html. It must be something that happens after Http404 is raised,
and my whole app/project are not involved anymore (except for
providing the template for the 404 page).

Kind regards,
Jan
> Thomas Guettler,http://www.thomas-guettler.de/

Karen Tracey

unread,
Oct 28, 2008, 3:09:22 PM10/28/08
to django...@googlegroups.com
On Tue, Oct 28, 2008 at 2:58 PM, janedenone <janed...@googlemail.com> wrote:

Hi,

it must be some kind of unhandled exception, but I fail to see where
it might occur. I now boiled down the app to a single URL pattern and
a single view:
[snipped]

If you configure ADMINS and EMAIL_HOST, etc. so that Django can successfully send email, then the traceback from the uncaught exception will be mailed to whatever email address you include in ADMINS.  Much easier than guessing what might be happening.

Karen

janedenone

unread,
Oct 28, 2008, 4:08:10 PM10/28/08
to Django users
Hi Karen,

I did that, but the message was never sent to me.

Anyway, when I create a new project with a single app, containing
nothing but a single view which raises a Http404, along with a
urlpattern which refers to that view – the 500 template is still
displayed when I request the URL.

This must be easily reproducible on any machine using the following
code:

# urls.py:
urlpatterns = patterns('myproject.myapp.views',
(r'^test/$', 'test'),
)

# views.py:
def test(request):
raise Http404

The same thing happens when I try any other URL: in debug mode, I get
the well-known

"Using the URLconf defined in djangoanke.urls, Django tried these URL
patterns, in this order:
^test/
The current URL, teste, didn't match any of these."

But with DEBUG = False, Django sends the 500.html template.

Clueless,
Jan

On 28 Okt., 20:09, "Karen Tracey" <kmtra...@gmail.com> wrote:

Karen Tracey

unread,
Oct 29, 2008, 12:42:37 AM10/29/08
to django...@googlegroups.com
On Tue, Oct 28, 2008 at 4:08 PM, janedenone <janed...@googlemail.com> wrote:

Hi Karen,

I did that, but the message was never sent to me.

That's the first problem I'd be working on fixing, then.  Long term you are likely to want that mechanism working, you've got a problem to solve right now for which it would be useful -- I'd be figuring out what's wrong with the settings that is causing the error email to go astray. 


Anyway, when I create a new project with a single app, containing
nothing but a single view which raises a Http404, along with a
urlpattern which refers to that view – the 500 template is still
displayed when I request the URL.

This must be easily reproducible on any machine using the following
code:

# urls.py:
urlpatterns = patterns('myproject.myapp.views',
      (r'^test/$', 'test'),
)

# views.py:
def test(request):
   raise Http404

Well yes if that is all you have in views.py then you'll get a 500 error because Http404 has not been imported.  But for the DEBUG=True case you'd also be seeing a debug page that starts with:

NameError at /test/

and goes on to complain that "global name 'Http404' is not defined" instead of:

Page not found (404)

and that doesn't match what you said earlier so I'm unsure if you skipped over some details in reporting the code for your minimal example or what it is you see when you have DEBUG set to True. 

I assure you, if I insert "raise Http404" into the beginning of one of my views, I get my customized 404 template, not a 500 error.  Whatever is going on is something specific to your code (or 400.html template), not something fundamentally broken in Django when Http404 is raised with DEBUG set to False.

Karen

janedenone

unread,
Oct 29, 2008, 5:20:11 AM10/29/08
to Django users
Hi all,

I found the solution. Someone (not me!) changed the
django.views.defaults.page_not_found(). The first line of the method
definition read

def page_not_found(request, template_name='404_default.html'):

instead of

def page_not_found(request, template_name='404.html'):

Thanks to all who answered, now I'll go looking for the person who
messed with the development machine.

Sorry for creating so much trouble,
Jan

Thomas Guettler

unread,
Oct 29, 2008, 9:00:39 AM10/29/08
to django...@googlegroups.com

> Thanks to all who answered, now I'll go looking for the person who
> messed with the development machine.
>
Everybody can make mistakes. The root of the problem is (or was)
that you don't see tracebacks if settings.DEBUG=False.

I see them in the apache error log. I use mod_wsgi.

Thomas

--
Thomas Guettler, http://www.thomas-guettler.de/

janedenone

unread,
Oct 31, 2008, 4:43:01 AM10/31/08
to Django users


On 29 Okt., 14:00, Thomas Guettler <h...@tbz-pariv.de> wrote:
> > Thanks to all who answered, now I'll go looking for the person who
> > messed with the development machine.
>
> Everybody can make mistakes. The root of the problem is (or was)
> that you don't see tracebacks if settings.DEBUG=False.

The issue only occurred when DEBUG was set to False (with DEBUG =
True, the templates were never used).

Kind regards,
Jan
Reply all
Reply to author
Forward
0 new messages