from django.utils import simplejson

def form_errors_to_json(form):
    return simplejson.dumps({'success': False,
                             'errors': dict([(k, v[0]) for k, v in form.errors.items()])})

from django.utils import simplejson


def _errors_to_dict(form):
    return dict([(k, v[0]) for k, v in form.errors.items()])


def form_errors_to_json(form):
    if hasattr(form, '__iter__'):
        rv = {'success': False, 'errors': []}
        for f in form:
            rv['errors'].append(_errors_to_dict(f))
    else:
        rv = {'success': False, 'errors': _errors_to_dict(form)}
    return simplejson.dumps(rv)

Improve form_errors_to_json to handle multiple forms
Improve form_errors_to_json to handle multiple forms

  from django.utils import simplejson
  
+ 
+ def _errors_to_dict(form):
+     return dict([(k, v[0]) for k, v in form.errors.items()])
+ 
+ 
  def form_errors_to_json(form):
-     return simplejson.dumps({'success': False,
-                              'errors': dict([(k, v[0]) for k, v in form.errors.items()])})
+     if hasattr(form, '__iter__'):
+         rv = {'success': False, 'errors': []}
+         for f in form:
+             rv['errors'].append(_errors_to_dict(f))
+     else:
+         rv = {'success': False, 'errors': _errors_to_dict(form)}
+     return simplejson.dumps(rv)
  
Improve form_errors_to_json to handle multiple forms
## Code Before:
from django.utils import simplejson

def form_errors_to_json(form):
    return simplejson.dumps({'success': False,
                             'errors': dict([(k, v[0]) for k, v in form.errors.items()])})

## Instruction:
Improve form_errors_to_json to handle multiple forms
## Code After:
from django.utils import simplejson


def _errors_to_dict(form):
    return dict([(k, v[0]) for k, v in form.errors.items()])


def form_errors_to_json(form):
    if hasattr(form, '__iter__'):
        rv = {'success': False, 'errors': []}
        for f in form:
            rv['errors'].append(_errors_to_dict(f))
    else:
        rv = {'success': False, 'errors': _errors_to_dict(form)}
    return simplejson.dumps(rv)
