django on nginx & apache : where to handle 404 & 500 error? - django

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.

I didn't know how to configure 404 & 500 errors in django.
Thanks to "namnatulco" who helped me.
Here are the steps:
Create 2 pages 404.html & 500.html
Place them in your modules template folder
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
To test 500 error, inside your view, in your render_to_response, give an incorrect URL.
That's it. You should be set.

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!

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.

Related

custom error handler django when using django-hosts

I have a system that has deployed django-hosts, all is working fine. Until now I have been handling 404 and 500 errors for production by utilizing the neat trick of just placing files 500.html and 404.html in the root of the templates folder. This very nicely handles these errors in production (debug=False).
However, now I want to get a little more creative with my error handling messaging and feedback to the user. I have other projects where I have created a custom view for the error handling.
e.g.
def custom_error_500(request, exception=None):
print ('error 500 ') #For debugging to verify view is being processed.
return render(request, "errors/500.html", status=500)
and then set this parameter at the bottom of the urls.py (appname is 'main')
handler500 = 'main.errorviews.custom_error_500'
This all works very nicely in a project that does NOT use django-hosts.
The advantage of creating the custom view is that the context is past to the RequestContext to the template, whereas by default not the case (see reference).
Now, I know with django-hosts here is magic going on in the way urls.py are processed for each host. Could this be the reason why?. I tried placing the hander500 setting in all the possible urls.py that I have in project, but I get the same result, which is the system just looks for the 500.html in the root and if it does not find it displays the standard built in Server Error 500 message.
Has anyone else had the same problem? I can not believe I am the only one trying to display a customer error message using django hosts :-).
Thanks in advance for any assistance!

Django Custom Error Handlers in Apache and mod_wsgi

I have custom 404 and 500 error handlers setup in django and they work in development, but with Apache they just show the default apache Internal Server Error page, even for 404 errors.
Here is my urls.py:
handler400 = 'views.error400'
handler403 = 'views.error403'
handler404 = 'views.error404'
handler500 = 'views.error500'
I've read of many ways to set up custom error pages in apache, but all of the approaches I tried with django did not work. What is the correct way to set this up for Apache/mod_wsgi and Django?
Do I remove those lines from my urls.py and add some lines to my apache.conf?
What lines should be added to apache.conf as I'm not sure how to access views from the apache configuration file and the normal urls.py routing for Django is not working for the custom error pages since as I suspect Apache is intercepting the requests and providing it's own pages.
I just want to use my templates for my custom error pages (which include inheriting from a base template).
How can I achieve this, I can't find a decent answer anywhere?
Thanks
EDIT:
I tried editting the Apache config to add ErrorDocument 500 /templates/500.html and such but it did not work.
I currently have the error page in my templates directory, they each inherit from a base template. Also, they each have their own view to handle the responses.
But no matter what I do, removing those lines from urls.py, adding stuff to apache config, etc. I can't get anything but the typical Internal Server Error apache page to display on any error, even one's that aren't 500 errors.
What can I do to get this working? Worked fine on the django development server, but as soon as apache was thrown into the mix I can't get it to work using either apache or django, I must be missing something but what?
You can remove those lines from urls.py. Django will look up to your templates to get those.
Just put 404.html and 500.html files into your_app/templates/ directory.

Using nginx/fcgi/django, I have form posts that give a 504 gateway time-out

I have an app that uses Django with FCGI on nginx. I'm using the third-party apps like James Bennett's django-registration and django-messages from the Pinax Project. Both of these apps have forms that are submitted and save data into the database, then redirect on to a new URL.
My issue seems to be that the .save() method on any of the forms in these apps cause a 504 gateway time-out when the forms are submitted. All of the data is saved in the database as it should be, however neither seems to ever return anything to the app after the form is saved.
I've done some logging at various points in the code and there are no errors. It's as if the save() methods on the form or the models the forms are connected to simply never return anything--error or otherwise.
With this lack of detail, an answer might be a dream for me, but just a nudge in the right direction or a way to diagnose the issue more completely would be fantastic.
Typically 504's in nginx happen due to timeout between nginx and the fastcgi process. You may want to take a look at your nginx settings and up the fastcgi_read_timeout setting?
Your nginx error log will typically provide a bit more information as to why things are not working as well. If you're on a *nix distro it's typically in "/var/log/nginx/error.log"
Turns out the problem was completely unrelated to nginx, but was Django having a timeout when trying to send an email. Unfortunately, it doesn't drop an error message or any indication that it can't connect to the email server.

Django Admin redirects not working

I am using the latest checkout of the django trunk - when I am in the admin on the "change" page for an object/item, there is a nice little link that says "view on site".
The link points to a url such as:
http://example.com:8888/admin/r/22/15/
However, when I click on that link (or enter that link into my browser) I get redirected to:
http://example.com:8888//example.com:8888/video/15
Which isn't a valid url - but it's really, really close ... the same is happening when I try to get_absolute_url for a comment. I get the short little redirect but it doesn't take me to the right page.
Any ideas why this is happening?
Additional Info (edit):
have tried with 1.1.1 (same problem)
I have one site listed under sites which is 'http://example.com:8888'
I thought maybe it had to do with the port number at the end - but what is strange is the my object's get_absolute_url works without a hitch.
I think the problem is coming from django.contrib.contenttypes.views.shortcut which is doing some funny appending business to handle cross-site things ... which I don't quite understand.
get_absolute_url (for objects)
#models.permalink
def get_absolute_url(self):
return ('video_detail', [str(self.id)])
I figured it out: my site's 'domain name' was listed as:
http://example.com
But the 'http' is what is throwing it off. When I removed it and just listed it as:
example.com
It works. I just have to update my email templates to include the 'http', I think ... unless django comes with a built in for adding that in its sites package. Off to investigate ...

Django, from php to Django

I have a website done with Django, that was previously done with PHP and CodeIgniter. I've moved the website to Webfaction, changed the DNS and all other configurations, but now my email is full of errors like this:
Error (EXTERNAL IP): /index.php/main/leer/7497
I don't know why the Django app is looking for pages from the PHP app, specially since the PHP app was in another host.
Are those URLs from your old site? That's probably a case of people having stale bookmarks, trying to navigate to them, and getting 404s. You might want to consider catching those, and redirecting to the new URL with response code 302.
I can't imagine those errors are caused by Django (except in the sense that the reports are from Django reporting 404s, which it does for free).
I agree with above. Just want to add you should use django.contrib.redirects to move the redirects.
You can read more about it here