How to receive server errors in AJAX request in Django
I am using this code
if form.is_valid():
form.save()
if request.POST.get('ajax') == 'true':
return HttpResponse('Data Entered Successfully') # Redirect after POST
else:
return HttpResponseRedirect('/thanks/') # Redirect after POST
I am submitting the form via jquery POST method.
The problem is whenever i have some error l开发者_StackOverflow社区ike indentation , or model error then i don't get anything back. i had to test the page by disabling ajax and submit via normal method and then i see the yellow django error page and then i correct the error.
is there any way that Even if i get any error i see that as response to jquery
In general you could register your own handler500 and test if the request is ajax and then from that return an ajax error response that indicates what happened if DEBUG is True, or just gives a general error if DEBUG is False.
The other options are to either use Firebug or Chrome Inspector to browse the Ajax response, or to setup some form of logging that logs the errors which is useful even when the site is deployed in order to do a post mortem on any errors that might occur.
Django itself has built in logging capabilities, one of which is sending an email to admins, but you can define your own using Django's integration with Python's built in logging. One option for a handler for these is django-sentry which I use on all my production sites to record any error that happens and also includes the ability to override the handler500 to give the end user a reference number that can be used to look up their exact error instance.
Sentry comes with 2 apps, one is the server, and one is the client. In a typical production setting when you have multiple machines serving the same site, you will have a dedicated sentry server that only does sentry. However for a single site having it integrated in with the site is ok.
To configure sentry you'll need to add both sentry
(the server) and sentry.client
(the client) to your INSTALLED_APPS
. Then you'll need to configure your logging.
Here is an example:
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"handlers": {
"mail_admins": {
"class": "django.utils.log.AdminEmailHandler",
"level": "ERROR"
},
"sentry": {
"class": "sentry.client.handlers.SentryHandler",
"level": "ERROR",
}
},
"loggers": {
"django.request": {
"handlers": ["sentry", "mail_admins"],
"level": "ERROR",
"propagate": True
}
},
}
The above example will still email admins if an error occurs and DEBUG = False
. If you do not want it to do that, remove mail_admins
from the handlers on django.request
.
A while ago I wrote a small utility called django-ajaxerrors which is designed precisely for this scenario. It's made of middleware that hooks into error responses, determines if they were the result of an AJAX request, and opens django technical error page in a new browser window (so the original page which issued the request remains the same).
I don't know how people do AJAX and django without it, but I maybe I'm not entirely objective here...
精彩评论