Reputation: 4014
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.
Upvotes: 4
Views: 3685
Reputation: 4014
I didn't know how to configure 404 & 500 errors in django. Thanks to "namnatulco" who helped me.
Here are the steps:
404.html
& 500.html
In your modules urls.conf
, enter these two lines:
handler404 = "myproject.mymodule.views.redirect_page_not_found" handler500 = "myproject.mymodule.views.redirect_500_error"
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));
Test it by giving some incorrect URL e.g. - www.mydomain.com/aaaaaaaaaaaaaaaa
render_to_response
, give an incorrect URL.That's it. You should be set.
Upvotes: 4
Reputation: 375932
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!
Upvotes: 3
Reputation: 17713
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.
Upvotes: 1