Skip to content

Instantly share code, notes, and snippets.

@zen4ever
Created April 25, 2012 22:55
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 zen4ever/2494169 to your computer and use it in GitHub Desktop.
Save zen4ever/2494169 to your computer and use it in GitHub Desktop.
Decorator for using form with django-piston and JSON-encoded POST body
from django.utils import simplejson as json
from piston.decorator import decorator
def validate_json(form_class, class_method=True):
@decorator
def wrap(f, *args, **kwargs):
if class_method:
request = args[1]
else:
request = args[0]
form = form_class(json.loads(request.raw_post_data))
if form.is_valid():
setattr(request, 'form', form)
return f(*args, **kwargs)
else:
return {'errors': form.errors}
return wrap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment