views:

106

answers:

3

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.

A: 

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
+1  A: 

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
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
A: 

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.

PlanetUnknown