title: update a django form with jquery/ajax
body: <p>I want to update a form on the change event of my drop down list.</p>
  
  <p>Here is my view:</p>
  
  <pre><code>from django.utils import simplejson
  response_dic={}
  #drop down list
  actToValidateId=add_form.cleaned_data['actsToValidate'].pk
  act=ActsIdsModel.objects.get(id=actToValidateId)
  ids_form = ActsIdsForm(instance=act)
  
  ids_form_dic={}
  for field in ids_form.fields:
      ids_form_dic[field]=getattr(act, field)
  response_dic['ids_form']=ids_form_dic
  response_dic['act']=dict([(attr, getattr(act, attr)) for attr in [f.name for f in act._meta.fields]])
  
  return HttpResponse(simplejson.dumps(response_dic), mimetype="application/json")
  </code></pre>
  
  <p>Here is the javascript code:</p>
  
  <pre><code>function display_act_ids(choice)
  {
      $form=$('#act_ids_form');
      var form_data = $form.serialize();
      $.ajax
      ({
          url: $form.attr('action'),
          type: 'POST',
          dataType: 'json',
          data: form_data,
          success: function(result)
          {
              alert(result.ids_form.fileNoCelex);
          }
      });
  
      //don't submit the form
      return false;
  }
  </code></pre>
  
  <p>Now, two problems:</p>
  
  <p>1/ If i want to assign the corresponding values to my form controls, I can update the success function as below:</p>
  
  <pre><code>$("#myfield").val(result.ids_form.myfield);
  </code></pre>
  
  <p>But what if I have many fields to fill? Is there a function to do that automatically? Maybe a loop would do...</p>
  
  <p>2/My main problem: I use my act instance (and other variables) in lots of places in my template (not in my form). For example:</p>
  
  <pre><code>{% if act.councilPath %}
      &lt;div class="row-fluid"&gt;{{ act.councilPath }}&lt;/div&gt;
  {% endif %}
  </code></pre>
  
  <p>In such a way, it is impossible to use ajax. Which means I have to rewrite my template to have it work. For example:</p>
  
  <pre><code>&lt;div id="council_path" class="row-fluid"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>And in my success function:</p>
  
  <pre><code> $("#council_path").html(result.act.councilPath);
  </code></pre>
  
  <p>This would be very long to update. Is there a better way to do, such as an ajax "post and load"?</p>
  
  <p>Please tell me if I am not clear.</p>