title: Help understanding a Django view
body: <p>I am trying to follow the code listed on <a href="https://github.com/alex/django-ajax-validation/blob/master/ajax_validation/views.py" rel="nofollow">https://github.com/alex/django-ajax-validation/blob/master/ajax_validation/views.py</a></p>
  
  <p>I have been able to understand a small chunk of it. I have added comments stating my understanding of what is happening. </p>
  
  <p>I would really appreciate some assistance on questions I listed in comments next to the lines I couldn't quite follow. </p>
  
  <pre><code>def validate(request, *args, **kwargs):
  
      # I thing it is some sort of initializations but I cannot really understand what's happening
      form_class = kwargs.pop('form_class')
      defaults = {
          'data': request.POST
      }
      extra_args_func = kwargs.pop('callback', lambda request, *args, **kwargs: {})
      kwargs = extra_args_func(request, *args, **kwargs)
      defaults.update(kwargs)
      form = form_class(**defaults)
  
      if form.is_valid(): #straightforward, if there is no error then the form is valid
          data = {
              'valid': True,
          }
      else:
      # if we're dealing with a FormSet then walk over .forms to populate errors and formfields
          if isinstance(form, BaseFormSet):  #I cannot really understand what is BaseFromSet
              errors = {}
              formfields = {}
              for f in form.forms: # I am guessing that this is for when there are multiple form submitted for validation
                  for field in f.fields.keys(): # I think he is looping over all fields and checking for error. what does add_prefix () return? and what is formfields[]?
                      formfields[f.add_prefix(field)] = f[field]
                  for field, error in f.errors.iteritems():
                      errors[f.add_prefix(field)] = error
              if form.non_form_errors():
                  errors['__all__'] = form.non_form_errors() # what is the '__all__'?
          else:
              errors = form.errors
              formfields = dict([(fieldname, form[fieldname]) for fieldname in form.fields.keys()])
  
      # if fields have been specified then restrict the error list
          if request.POST.getlist('fields'): # I am having a hard time understanding what this if statement does.
              fields = request.POST.getlist('fields') + ['__all__']
              errors = dict([(key, val) for key, val in errors.iteritems() if key in fields])
  
          final_errors = {} # here the author of this code totally lost me.
          for key, val in errors.iteritems():
              if '__all__' in key: 
                  final_errors[key] = val
              elif not isinstance(formfields[key].field, forms.FileField):
                  html_id = formfields[key].field.widget.attrs.get('id') or formfields[key].auto_id
                  html_id = formfields[key].field.widget.id_for_label(html_id)
                  final_errors[html_id] = val
          data = {
              'valid': False or not final_errors,
              'errors': final_errors,
          }
  
      json_serializer = LazyEncoder() # Why does the result have to be returned in json?
      return HttpResponse(json_serializer.encode(data), mimetype='application/json')
  
  validate = require_POST(validate) # a decorator that requires a post to submit
  </code></pre>
  
  <p>LazyEncoder</p>
  
  <pre><code>class LazyEncoder(JSONEncoder):
      def default(self, obj):
          if isinstance(obj, Promise):
              return force_unicode(obj)
          return obj
  </code></pre>