title: Django form submission - avoid page redirect
body: <p>I am using a django form to update some details. once the save changes is clicked the ajax call is made, view executed, ajax response sent, success code executed n the page reloads again. n on reload as form is no longer valid, the else part of from.is_valid() is executed.</p>
  
  <pre><code>if form.is_valid:
      #do something
      return HttpResponse(simplejson.dumps(response), mimetype='application/json')
  else:
      #do something
      return render_to_response('ts.html', {'form': form}, context_instance = RequestContext(request))
  </code></pre>
  
  <p>I want to avoid the page reload on the successful form submission and jus return ajax response. How can achieve that?</p>
  
  <p>I have changed my code and here is the view, template and jquery.</p>
  
  <pre><code>if request.POST:
      if form.valid():
           if credentials.correct():
                 resp = {'success': True}
                 return HttpResponse(simplejson.dumps(resp), mimetype='application/json')
           else:
                resp = {'success': False}
                return HttpResponse(simplejson.dumps(resp), mimetype='application/json')
      else:
           print "error in form"
           return render(request, 'manage_accounts.html', {'creds': creds, 'form': form})
  else:
       form = SomeForm()
       return render(request, 'manage_accounts.html', {'creds': creds, 'form': form})
  </code></pre>
  
  <p>Template</p>
  
  <pre><code>&lt;form action="/accounts/" method="POST" id="bitlychangeform"&gt;
    &lt;div id="ajaxwrapper"&gt;
  {% csrf_token %}
  {{ form.non_field_errors }}
  &lt;!--{% include "Change_bitly_form.html" %}--&gt;
  {{ form.as_p }}
        &lt;div class="clearfix"&gt;
          &lt;label&gt;&lt;/label&gt;
              &lt;div class="input" style="float:right"&gt;
              &lt;input type="submit" id="save" value="Save Changes" class="btn btn-primary  "/&gt;
          &lt;/div&gt;
       &lt;/div&gt;
    &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Jquery:</p>
  
  <pre><code>$(function(){
  var form = $('#bitlychangeform');
      form.submit(function(e) {
      jQuery("#save").attr('disabled', true)
      jQuery("#ajaxwrapper").load(
            form.attr('action') + ' #ajaxwrapper',
            form.serializeArray(),
            function(data) {
                jQuery("#save").attr('disabled', false)
        alert(data);
            });
    return false;
        e.preventDefault(); 
    });
  });
  </code></pre>
  
  <p>The page reload doesn't occur when i use Django form as {{ form.as_p }} but when i use a "custom template for form with fields as password char fields with ** and editable only after one clicks edit button", the form.valid() returns false. </p>
  
  <p>I require that the django form functions as my custom template for form as i desire. Can anyone guide me.</p>