jaime jaime - 18 days ago 5
Python Question

can i send a message to a django template in an HttpResponseBadRequest?

Can i send an error message attached to an HttpResponseBadRequest which will be displayed in my template?
ie.

view.py

def myView():
....
return HttpResponseBadRequest(msg)


template.html

$.get(url,function(data){
...
})
.done(function(){
...
})
.fail(function(){
alert(msg);
})


This is what happens

Answer

You've defined a global JS variable somewhere called msg.

If you want to refer to the data returned by the Ajax error, you need to accept it as a parameter in your fail function:

.fail(function(jqXHR, textStatus, error) {
    alert(jqXHR.responseText);
})
Comments