I’ve been trying to get some Ajax stuff to work in my Django application, but it has been difficult because for some reason syntax and type errors are not causing a server crash and are failing silently. How could this be?
JavaScript (jQuery):
add_foo = function() {
var numfoos = $("div#foos_container > div.foo").length + 1;
var foo_id = numfoos + "-foo";
var div = $("<div></div>").attr("id",foo_id).addClass("foo");
div.load("http://localhost:8000/ajax/get/url/");
div.appendTo("div#foos_container");
};
Python:
def ajax_add_foo(request):
print request.session['editing_foos'].keys()
keys = request.session['editing_foos'].keys()
if len(keys) == 0:
next_key = 1
else:
next_key = max([ id_from_prefix(key) for key in keys ])
print next_key
form = FooForm(prefix=next_key)
print next_key
request.session['editing_foos'].update( {create_prefix(FooForm, next_key) : -1 } ) # This foo is new and has no pkey
print request.session['editing_foos']
return render_to_response( 'bar/foo_fragment.html',
{'fooform' : fooform, },
context_instance=RequestContext(request))
This is probably because server returns error code (500) and your jQuery cod doesn’t do anything on error. Could you post $.get or $.post code you are using?
EDIT: If you are using $.load, there is a place for a callback function, that you can create to display errors. It’s pretty simple, you need to define a function similar to this:
This way you will put error message into the whole document and see it. I don’t exactly know, if the above works, because I can’t test it, but you should be able to construct something working on this.