Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from app.lib.shortcuts import render_to_json_response
# a mixin to add AJAX support to a form
# must be used with an object-based FormView (e.g. CreateView)
class AjaxFormResponseMixin(object):
def form_invalid(self, form):
return render_to_json_response(form.errors, status=400)
def form_valid(self, form):
# save
self.object = form.save()
# initialize an empty context
context = {}
# return the context as json
return render_to_json_response(self.get_context_data(context))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment