0

I'm trying to create the custom 404.html page for the HTTP 404 error.

For this url 127.0.0.1:8000/polls/Books/dsfdsf/ it is showing the Page not found (404) default debugging page, while DEBUG=True. For same url i want to create custom 404 page.

i have gone through the Django Docs for this.

Following steps i have taken:

1). DEBUG=False in settings.py

2). handler404 = 'pollsite.views.custom_404' in urls.py

3). views.py

def custom_404(request):
    return render('404.html', context_instance=RequestContext(request))

4). i have placed the 404.htmlfile in template directory , TEMPLATE_DIRS

now i expect the custom error page 404.html to appear when i tries to access 127.0.0.1:8000/polls/Books/dsfdsf, right? Instead i'm getting the TemplateDoesNotExist: 500.html

Even valid url showing the TemplateDoesNotExist: 500.html

Why it is not generating HTTP 404 error, and thereby invoking custom 404.html file ?

I tried solution mentioned for question1, and question2 but still geting annoying error TemplateDoesNotExist: 500.html

please point out what is the issue?

EDIT: stacktarce of error

Traceback (most recent call last):
  File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 241, in __call__
    response = self.get_response(request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 153, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 228, in handle_uncaught_exception
    return callback(request, **param_dict)
  File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py", line 91, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/views/defaults.py", line 32, in server_error
    t = loader.get_template(template_name) # You need to create a 500.html template.
  File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py", line 145, in get_template
    template, origin = find_template(template_name)
  File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py", line 138, in find_template
    raise TemplateDoesNotExist(name)
TemplateDoesNotExist: 500.html

.........................Solution:

1) in settings.py set ALLOWED_HOSTS = ['127.0.0.1']

2) in views.py return render_to_response('404.html')

Community
  • 1
  • 1
navyad
  • 3,752
  • 7
  • 47
  • 88
  • It sounds like your app is actually throwing a 500 error but can't find a template for the 500 error. Have you checked your logs to see what the error is? – Timmy O'Mahony Oct 08 '13 at 18:07
  • i have edited my post for error i'm getting – navyad Oct 08 '13 at 18:30
  • Yes, the error is that because you have `DEBUG=False`, an error it being thrown but there is no 500 template in your `templates` folder. You need to add a `500.html` there just like you have the `404.html`. That won't solve your problem though as there is likely an error in your setup causing the 500 – Timmy O'Mahony Oct 08 '13 at 18:40

1 Answers1

0

In settings.py there is a setting:

 ALLOWED_HOSTS = []

If Debug = False then Django looks at ALLOWED_HOSTS. This setting needs to contain your IP address. You can also set it to:

 ALLOWED_HOSTS = ['*']

This works fine in the development environment, but should not be used in production. This is most likely your issue and why Django is throwing a 500.html server error when trying to load that page.

Aaron Lelevier
  • 19,850
  • 11
  • 76
  • 111