4

In one of my views I have the code:

raise Http404

When DEBUG=False Django renders the template 500.html instead of the correct 404.html! I can't understand why!

EDIT: When DEBUG=True I get the standard one (by Django)

Page not Found (404)
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

What's more is that in the runserver console I see clearly the 404 code. Instead, when setting DEBUG=False in the console I get a 500!! This is so odd.

EDIT 2: If I place a print statement just before raising Http404 I see the message when DEBUG=True but not when it is False!

EDIT 3: I can confirm that when DEBUG=False the statement raise Http404 is never reached. How can it be possible??

Update

Actually with DEBUG=False at every URL I get a 500. While with DEBUG=True this does not happen. How can it possibly be?? It should run the same. I'm starting to think it is a Django bug.

Community
  • 1
  • 1
rubik
  • 8,814
  • 9
  • 58
  • 88
  • 3
    One raison may be because your 400.html template generates a 500 error, can you post your template ? – maazza Jun 29 '13 at 16:29
  • And what happens with DEBUG=True ? – bruno desthuilliers Jun 29 '13 at 16:32
  • @maazza: I doubt it. I created a new 404.html and wrote in it just 404. Yet, it didn't get rendered. – rubik Jun 29 '13 at 16:33
  • You probably dont have your 404.html in the right place ? – karthikr Jun 29 '13 at 16:36
  • @karthikr: I have it at the root of my templates directory, as the Django docs say. Proof of this is that if I call: `loader.render_to_string('404.html')` I see the right code. – rubik Jun 29 '13 at 16:40
  • your error with debug=false may be due to the allowed_hosts setting http://stackoverflow.com/questions/15128135/django-setting-debug-false-causes-500-error – maazza Jun 29 '13 at 18:54
  • Wow, it works. I really hadn't see that. Thank you so much. Would you add that as an answer so that I can accept it? – rubik Jun 29 '13 at 19:20

1 Answers1

4

Your error with debug=false may be due to the allowed_hosts setting.

see : Setting DEBUG = False causes 500 Error

It is new in django 1.5

ALLOWED_HOSTS required in production

The new ALLOWED_HOSTS setting validates the request’s Host header and protects against host-poisoning attacks. This setting is now required whenever DEBUG is False, or else django.http.HttpRequest.get_host() will raise SuspiciousOperation. For more details see the full documentation for the new setting.

https://docs.djangoproject.com/en/1.5/releases/1.5/#allowed-hosts-required-in-production

Community
  • 1
  • 1
maazza
  • 7,016
  • 15
  • 63
  • 96