4

I know there is 404 error handling in django. But is it better to just put that config in nginx ?

This ST thread has the solution for putting it. - http://stackoverflow.com/questions/1024199/nginx-customizing-404-page

Is that how everyone handles it when using nginx ?
I have created my own 404.html & 500.html in the sites theme, want to display them.

pnuts
  • 58,317
  • 11
  • 87
  • 139
PlanetUnknown
  • 3,956
  • 4
  • 49
  • 67
  • Especially for 404 pages it makes more sense to me to display it using django, since i find it useful to eg. have a navigation on it, that is generated using functionality offered by django (eg. template tags). – Bernhard Vallant Sep 24 '10 at 18:26
  • My thought was of not adding any logic in django. If a 404 occurs, the webserver should detect it and display my static page. Same for 500. – PlanetUnknown Sep 25 '10 at 12:18

3 Answers3

4

I didn't know how to configure 404 & 500 errors in django. Thanks to "namnatulco" who helped me.

Here are the steps:

  1. Create 2 pages 404.html & 500.html
  2. Place them in your modules template folder
  3. In your modules urls.conf, enter these two lines:

    handler404 = "myproject.mymodule.views.redirect_page_not_found"   
    handler500 = "myproject.mymodule.views.redirect_500_error"
    
  4. In your view, define the functions

    def redirect_page_not_found(request):
      return render_to_response('logreg/404.html', {}, context_instance=RequestContext(request));     
    def redirect_500_error(request):
      return render_to_response('logreg/500.html', {}, context_instance=RequestContext(request));     
    
  5. Test it by giving some incorrect URL e.g. - www.mydomain.com/aaaaaaaaaaaaaaaa

  6. To test 500 error, inside your view, in your render_to_response, give an incorrect URL.

That's it. You should be set.

Carlos Lima
  • 4,162
  • 29
  • 32
PlanetUnknown
  • 3,956
  • 4
  • 49
  • 67
3

You haven't mentioned any reasons why you would want to put these pages in the Nginx server. I would recommend keeping it with the rest of your site, that is, on the Django server. Moving part of your site to the Nginx server is a good idea to solve scalability problem, but complicates your deploy. I certainly hope you aren't seeing a significant fraction of your site's traffic going to your error pages!

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • Thanks, that makes sense, also folks on #django helped me understand that I can customize it so well. I'll put how I implemented it below. But will select this as an answer. – PlanetUnknown Sep 26 '10 at 21:53
1

I recommend using an in-Django 404/500 handler. You can deliver meaningful alternate nav suggestions in a page style that is consistent with the rest of your site.

Make sure you do not return a page talking about the error but sporting a 200 return status -- human will understand it's an error, but programmatic access will not. I'm avoiding saying "search engines" here, but the truth is that they will probably represent 98%+ of your non-human visitors. See HttpResponse subclasses for details.

Peter Rowell
  • 17,605
  • 2
  • 49
  • 65