Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Django debug information when using Ajax call?

Tags:

jquery

django

Is there a good way to get the debug information that Django provides when using jQuery to make Ajax calls? Right now when I make a call, I just see a http/200 server error in the python runserver window, but because the call is made through javascript, I don't get a debug page with all the information.

like image 556
victor Avatar asked Aug 29 '09 17:08

victor


3 Answers

You can inspect the contents of the response returned to your jQuery ajax call. Using a tool like Firebug can make this pretty easy.

Django will still return the debug page, it's just that it is responding to the ajax call rather than a regular browser request.

It's often a good technique to get your stuff working with regular requests, and then "ajaxify" them only once you are sure the server side code is working.

like image 89
TM. Avatar answered Oct 29 '22 13:10

TM.


You can use this snippet: http://www.djangosnippets.org/snippets/650/ to get plaintext tracebacks for viewing in firebug, instead of HTML.

like image 24
Alex Gaynor Avatar answered Oct 29 '22 13:10

Alex Gaynor


When you receive an AJAX error like that, you can replace your current document contents with the debug/error document returned from the server. For example, you can do something like the following:

$.ajax({
    url: 'failing_controller/',
    type: 'POST'
})
    .fail(function (jqXHR, textStatus, errorThrown) {
        document.open();
        document.write(jqXHR.responseText);
        document.close();
    })
    .success(function (data, textStatus, jqXHR) {
        // ... handle data ...
    });

This can be really helpful during development and debugging, because it lets you inspect the server state in real-time. You will probably want to replace the failure handler with something more proper in a production environment.

like image 21
Martin Thorsen Ranang Avatar answered Oct 29 '22 14:10

Martin Thorsen Ranang