Skip to content

Instantly share code, notes, and snippets.

@DrMartiner
Last active December 19, 2015 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DrMartiner/5927026 to your computer and use it in GitHub Desktop.
Save DrMartiner/5927026 to your computer and use it in GitHub Desktop.
AJAX form, who return errors in dictionary, else return HTTP 201 (code of success create)
# -*- coding: utf-8 -*-
import json
from django.http import HttpResponse
from django.views.generic import FormView
class BaseAJAXFormView(FormView):
mimetype = 'application/json'
def form_invalid(self, form):
errors = {}
for name in form.fields:
error = form.errors.get(name)
if error:
errors[name] = error[0]
return HttpResponse(json.dumps(errors),
status=200,
mimetype=self.mimetype)
def form_valid(self, form):
form.save()
return HttpResponse(status=201,
mimetype=self.mimetype)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment