Reputation: 35
I was able to hook ajax with django, but I'm running into the following problem. In my ajax function, I have this:
function submitForm() {
var contactForm = $(this);
if ( !$('#senderName').val() || !$('#senderEmail').val() || !$('#message').val() ) {
$('#incompleteMessage').fadeIn().delay(messageDelay).fadeOut();
contactForm.fadeOut().delay(messageDelay).fadeIn();
} else {
$('#sendingMessage').fadeIn();
contactForm.fadeOut();
$.ajax( {
url: contactForm.attr( 'action' ) + "?ajax=true",
type: contactForm.attr( 'method' ),
data: contactForm.serialize(),
success: submitFinished
} );
}
return false;
}
And the view now looks like this:
def postMessageForm(request):
if request.POST:
print "POST"
if request.GET:
print "GET"
How do I tell the view to send the success variable back so the code continues? In php, it would be something like this: .... if ( isset($_GET["ajax"]) ) { echo $success ? "success" : "error";
Thanks in advance
Upvotes: 1
Views: 128
Reputation: 29511
hmmm is it
if 'ajax' in request.GET:
return HttpResponse(simplejson.dumps({'success':'success','other_stuff':}))
else:
return HttpResponse(simplejson.dumps({'success':'error','other_stuff':}))
But u can check whether request is ajax by using request.is_ajax()
Upvotes: 1