Skip to content

Instantly share code, notes, and snippets.

@felipe-prenholato
Created February 14, 2013 13:29
Show Gist options
  • Save felipe-prenholato/4952775 to your computer and use it in GitHub Desktop.
Save felipe-prenholato/4952775 to your computer and use it in GitHub Desktop.
{% spaceless %}
{# this is included in form.html and is loaded via ajax #}
{% load bootstrap_toolkit %}
{% if success %}
{# msg in form instead in next request #}
<div class="alert success alert-success">
<a class="close" data-dismiss="alert">×</a>
{{ success|safe }}
</div>
{% endif %}
{{ form|as_bootstrap }}
{% endspaceless %}
<!-- this is original form in html page -->
<form method="POST" action="{% url "your_update_form_url" pk=object.pk %}" class="form" id="myform">
<div class="form_fields">
{% with form=user_form %}
{% include "user_ajax_form.html" %}
{% endwith %}
</div>
<div class="form_buttons">
<button class="btn btn-primary" name="update" type="submit">Update</button>
<button class="btn btn-danger" name="reset">Reset</button>
</div>
</form>
(function($) {
// untested code, I changed variables names from working code
// this js post form via ajax and update .form_fields with rendered form in ajax_form.html
var $form = $("#myform");
$form.submit(function(e) {
e.preventDefault();
e.stopPropagation();
var $that = $(this);
$.post($that.attr('action'), $that.serializeArray(), function(data, status) {
$that.find(".form_fields").html(data.form);
});
return false;
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment