Obrigado pela dica Elyézer, mas existem vários forms no template. Se trata de uma tabela, onde no último <td> existe uma janela modal, o usuário clica e se abre um modal com os dados correspondentes de cada janela.
Consegui enviar o form via ajax e receber as mensagens de sucesso e erros, exceto a mensagem de erros existentes no form.
O response desta mensagem de sucesso é mostrado corretamente na janela modal:
data = {'reposicao':'<strong>Janela atualizada com sucesso.</strong>'}
return HttpResponse(simplejson.dumps(data), mimetype='application/json')
Mas para os erros no form não, só aparece uma chave "}" na janela modal.
data = dict((key, [unicode(v) for v in values]) for key, values in form.errors.items())
return HttpResponse(simplejson.dumps(data))
O response da views é:
{"reposicao": ["Este campo \u00e9 obrigat\u00f3rio."]}
Imagem:
http://postimage.org/image/wccoobh45/
Se eu colocar:
return HttpResponse(simplejson.dumps(data), , mimetype='application/json')
Este é o erro:
http://postimage.org/image/qqzgc20dv/
A função JQuery:
$(function () {
var $modal = $('div').css('display'); /// search in all modals
if($modal == 'block') { // check the modal as block style
var $form = $(this).find('form'); // find the form of modal with style block
var $message = $(this).find('div#message'); // find the form in modal
$($form).submit(function() { // catch the form's submit event
$.ajax({ // create an AJAX call...
data: $(this).serialize(), // get the form data
type: $(this).attr('method'), // POST
url: $(this).attr('action'), // the file to call
success: function (data) {
$.each(data, function(key, msg) {
$($message).html(msg); // update the DIV message
$($form).resetForm(); // reset the form
});
}
});
return false;
});
};
});
Sabe dizer o pq de não mostrar corretamente a mensagem de erros no form?
Abraço!