Reputation: 5578
I'm using request.POST.get('...')
inside my Django decorator (@save_post_request
) whenever my form is submitted, on each tentative I get this same error
(error with request.<anything>
):
AttributeError: 'collectData' object has no attribute 'POST'
My decorator is called on top of a post()
function inside CollectData
classBasedView.
#views.py
class collectData(View):
template_name = 'collect_data.html'
context = {...}
def get(self, request, *args, **kwargs):
...
return render(request, self.template_name, self.context)
@save_post_request
def post(self, request, *args, **kwargs):
...
return redirect(reverse('collectData'))
#decorators.py
def save_post_request(function):
def wrap(request, *args, **kwargs):
title = request.POST.get('title') # <---
...
return function(request, *args, **kwargs)
wrap.__doc__ = function.__doc__
wrap.__name__ = function.__name__
return wrap
I'm not sure if a decorator can be called like so using classBasedViews
, but I think it should be right, what is my mistake?
Upvotes: 1
Views: 978
Reputation: 47354
First argument of inner function should be self
:
def save_post_request(function):
def wrap(self, request, *args, **kwargs):
title = request.POST.get('title') # <---
...
return function(self, request, *args, **kwargs)
wrap.__doc__ = function.__doc__
wrap.__name__ = function.__name__
return wrap
Upvotes: 4