Skip to content

Instantly share code, notes, and snippets.

@jhgaylor
Forked from zmsmith/AjaxFancyBox.html
Created July 20, 2012 02:16
Show Gist options
  • Save jhgaylor/3148247 to your computer and use it in GitHub Desktop.
Save jhgaylor/3148247 to your computer and use it in GitHub Desktop.
def the_display_view(request):
form = settingsapp.views.ajax_form(request)
our_data_dict = {}
our_data_dict['form'] = form
...
...
return HttpResponse('template.html', our_data_dict)
<div class="yourFancyBoxClass">
{% if success %}
<h2>It Worked!</h2>
{% else %}
<form action="{% url ajax_form %}">
{% form %}
</form>
{% endif %}
</div>
$(".yourLaunchClass").fancybox({onComplete:function (){
//grab this function so that we can pass it back to
//`onComplete` of the new fancybox we're going to create
var func = arguments.callee;
//bind the submit of our new form
$('.yourFancyBoxClass form').submit(function(){
//this is strictly cosmetic
$.fancybox.showActivity();
var data = $(this).serialize();
var url = $(this).attr('action')
//post to the server and when we get a response,
//draw a new fancybox, and run this function on completion
//so that we can bind the form and create a new fancybox on submit
$.post(url, data, function(msg){$.fancybox({content:msg,onComplete:func})});
return false;
});
}
});
def ajax_form(request):
context = {}
if request.method == "POST":
form = myForm(request.POST)
if form.is_valid():
form.save()
context['success'] = True
####
return True
####
else:
form = WaitingListEntryForm()
context['form'] = form
return form
#return render_to_response("AjaxFancyBox.html", context, context_instance=RequestContext(request))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment