Skip to content

Instantly share code, notes, and snippets.

@sshirokov
Created September 26, 2009 16:04
Show Gist options
  • Save sshirokov/194293 to your computer and use it in GitHub Desktop.
Save sshirokov/194293 to your computer and use it in GitHub Desktop.
@require_POST_or(url_name_or_callable): Run this method if POST, call another or redirect away otherwise.
from django.core.urlresolvers import reverse
from django.views.decorators.http import require_POST
from django.http import HttpResponseRedirect, HttpResponseNotAllowed
def require_POST_or(other):
def _d_closure(func):
def wrapped_func(request, *args, **kwargs):
r = require_POST(func)(request, *args, **kwargs)
if type(r) == HttpResponseNotAllowed:
if callable(other): return other(request, *args, **kwargs)
else: return HttpResponseRedirect(reverse(other))
return r
wrapped_func.__name__ = func.__name__
wrapped_func.__doc__ = func.__doc__
return wrapped_func
return _d_closure
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment