Last active
December 19, 2015 08:39
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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