1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | def formview(func):
"""
Decorator for separating GET and POST implementations
Example:
@formview
def viewfunction(request):
#Write code which needs to be called for both
def get():
#return GET view to be rendered
def post():
#return view after form POST
#Make sure to return get, post functions in the same order
return get, post
"""
def wrapped(request, *args, **kargs):
get, post = func(request, *args, **kargs)
if request.method == "POST":
return post()
else:
return get()
return wrapped
|
More like this
- View Permission Decorator Helper by jgeewax 4 years, 11 months ago
- LoginAsForm - Login as any User without a password by johnboxall 4 years ago
- Decorator that limits request methods by schinckel 3 years, 11 months ago
- Simple views method binding by Spike^ekipS 5 years ago
- View decorator providing confirmation dialog by rudyryk 3 years, 4 months ago
Comments
Nice example of "Simple is better than complex". I like it.
#
"Django does not have a clean, built-in mechanism to separate GET and POST implementations."
Yes it does. Use a class based view and derive your class from ProcessFormView. Then, simply define get() and post() methods.
See the Django ProcessFormView docs.
#