How to POST a django form with AJAX & jQuery
原文地址:http://stackoverflow.com/questions/7335780/how-to-post-a-django-form-with-ajax-jquery
html:
<script type="text/javascript"> var frm = $('#FORM-ID'); frm.submit(function () { $.ajax({ type: frm.attr('method'), url: frm.attr('action'), data: frm.serialize(), success: function (data) { $("#SOME-DIV").html(data); }, error: function(data) { // Display form validation errors like the image below var form_errors = data.msg; for(var fieldname in form_errors) { var error = form_errors[fieldname]; $('#id_'+fieldname).parent().addClass("has-error"); if ($('#id_'+fieldname).nextAll().length < 1) $('#form input[name='+fieldname+']').after("<span class='help-block'>"+error+"</span>"); } } }); return false; }); </script>
view:
form error显示效果: