title: Handling Django Model Form Error in Ajax Submit
body: <p>I want to sumbit a model form with ajax and using model form validation messages:</p>
  
  <pre><code>class ComplaintForm(forms.ModelForm):
    class Meta:
        model = Complaint
        fields = [
            'title','body'
        ]
  
  
    def clean_body(self):
        form_data = self.cleaned_data
        body = self.cleaned_data.get('body', False)
        if len(body) &lt; 2:
            raise forms.ValidationError(u'Please Add Complaint')
        return body
  
    def clean(self):
        cd = self.cleaned_data
        return cd
  </code></pre>
  
  <p>In my view:</p>
  
  <pre><code>def forms(request):
    form = ComplaintForm()
    if request.method == "POST":
        if request.is_ajax():
            form = ComplaintForm(request.POST)
            if form.is_valid():
                c = form.save(commit=False)
                c.user_ip = get_client_ip(request)
                c.user = request.user
                c.news =  news
                c.save()
                data = serializers.serialize('json', [c,])
            else:
                data = json.dumps([v for k,v in form.errors.items()])
                return HttpResponseBadRequest(data, mimetype='application/json')
            return HttpResponse(data, mimetype='application/json')
  
    else:
        form = ComplaintForm()
  
    return render_to_response('main/form.html', {'form': form},
                            context_instance=RequestContext(request))
  </code></pre>
  
  <p>But, my problem is how could I send data through HttpResponseBadRequest ?
  My js is:</p>
  
  <pre><code>$('.complaintform').submit(function(e){
      e.preventDefault();
      $.ajax({
          type: "POST",
          url: "/form/",
          dataType: "json",
          data: $(this).serialize(),
          success: function(data) {
              $('p').html('ok');                
          },
          error: function(data) {
             //how could i insert model form errors here?
  
          }
      });
  
  });
  </code></pre>