title: jquery ajax / django - present form in a bootstrap modal and re-show if validation was not successful
body: <p>my use case is:</p>
  
  <p>a) Present a form loaded via ajax in a bootstrap modal, the <a href="http://twitter.github.com/bootstrap/javascript.html#modals" rel="nofollow noreferrer">fancy overlay effect stuff..</a> . I followed these <a href="https://stackoverflow.com/questions/11276100/how-do-i-insert-a-django-form-in-twitter-bootstrap-modal-window">instructions</a>.</p>
  
  <p>This works fine. (see code below)</p>
  
  <p>b) Submit this form back to my Django app, try to validate it, and if it does not validate, re-show the form with the errors in the fancy bootstrap modal. </p>
  
  <p>I can reload the form via ajax, but I m not able to represent it again in the modal.</p>
  
  <p>Note: I did not include the view since it does nothing special. Only instantiating and validating the form.</p>
  
  <p>Quite a lot to read below, so just continue if you think the use case sounds interesting...</p>
  
  <p>My taskList.html looks like this:</p>
  
  <pre><code>&lt;table id="listItemTable" class="table table-bordered"&gt;
          &lt;thead&gt;
              &lt;tr&gt;
                  &lt;th&gt;Name&lt;/th&gt;
                  &lt;th&gt;Edit&lt;/th&gt;
              &lt;/tr&gt;
          &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;Task 1&lt;/td&gt;
              &lt;td&gt;&lt;a class="editItem" href="/update/item/1/"&gt;edit&lt;/a&gt;&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
  &lt;/table&gt;
  
  &lt;div class="modal hide" id="itemFormModal"&gt;&lt;/div&gt;
  &lt;div id="modalExtraJsPlaceholder"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>.js for loading the form + showing the bootstrap modal + binding form to a .jquery submit call:</p>
  
  <pre><code>$(document).ready(function() {
     modalConnect();
  });
  
  &lt;script type="text/javascript"&gt;
   //connects the modal load for each &lt;a&gt; with class editItem
   //Functionality 1
       //loads an item edit form from the server and replaces the itemFormModal with the form
       //presents the modal with $("#itemFormModal").modal('show');
   //Functionality 2
       //loads some extra js "modalExtraJsHtml"
       //calls the function "submitItemModalFormBind" which has been loaded via "modalExtraJsHtml"
  
   function modalConnect(){
      $(".editItem").click(function(ev) { // for each edit item &lt;a&gt;
      ev.preventDefault(); // prevent navigation
      url = ($(this)[0].href); //get the href from &lt;a&gt;
      $.get(url, function(results){
         var itemForm = $("#ajax_form_modal_result", results);
         var modalExtraJs = $("#modalExtraJs", results);
         //get the html content
         var modalExtraJsHtml = modalExtraJs.html();
         //update the dom with the received results
          $('#itemFormModal').html(itemForm);        
          $('#modalExtraJsPlaceholder').html(modalExtraJsHtml);
          $("#itemFormModal").modal('show');
          submitItemModalFormBind(); //bind loaded form to ajax call
       }, "html");
       return false; // prevent the click propagation
     })
  }
  &lt;/script&gt;
  </code></pre>
  
  <p>The itemForm returned from the view looks like this:</p>
  
  <pre><code>&lt;form id="#ajax_form_modal_result" class="well" method="post" action="/update/item/{{ item.id }}"&gt;
   &lt;div id="ajax_form_modal_result_div"&gt;
       &lt;div class="modal-header"&gt;
        &lt;button type="button" class="close" data-dismiss="modal"&gt;×&lt;/button&gt;
        &lt;h3&gt;Edit Item&lt;/h3&gt;
      &lt;/div&gt;
      &lt;div class="modal-body"&gt;
           {% csrf_token %}
           {{form.as_p}}
      &lt;/div&gt;
      &lt;div class="modal-footer"&gt;
           &lt;input class="btn btn-primary" type="submit" value="Save" /&gt;
           &lt;input name="cancel" class="btn" type="submit" value="Cancel"/&gt;
       &lt;/div&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Loading and showing the modal works fine. 
  But now comes the second part which does not work as expected. The issue is the following. If the form does not validates, the view returns the form. The returned form should be shown again in the bootstrap modal. But the result is that ONLY the form is presented in the browser, everything else is lost. No css, no table, only the form. Quite ugly.. Thus I did not achieve to update the ajax_form_modal_result_div. Can anyone help me out here what I m doing wrong..!?</p>
  
  <p>The view returns also the js function 'submitItemModalFormBind' which prevents the form default behavior and sends the form via ajax. </p>
  
  <pre><code>&lt;div id="modalExtraJs"&gt;
   //ajax bind for update item form visualized via modal
   function submitItemModalFormBind(){
       var url = "{% url updateItem item.pk %}";
       $('#ajax_form_modal_result').submit(function(){
   $.ajax({
      type: "POST",
      url: "{% url updateTask item.pk %}",
      data: $(this).serialize(),
      success:function(response){
          var div = $("ajax_form_modal_result_div", response);
          $('#ajax_form_modal_result_div').html(div);
       },
       error: function (request, status, error) {
           console.log("failure");
           console.log(request.responseText);
       }
  
             });
            });
          return false;
         }    
  &lt;/div&gt; 
  </code></pre>