python - Attribute Error when writing a captcha decorator -


i wrote captcha decorator...

def validate_captcha(view):     '''decorator validate captcha based on settings'''      def failure():         return httpresponse('there error, please refresh , try again')      def wrap(request, *args, **kwargs):         if request.method == 'post':              url = "https://www.google.com/recaptcha/api/siteverify"             values = {                 'secret': settings.google_recaptcha_secret_key,                 'response': request.post.get(u'g-recaptcha-response', none),                 'remoteip': request.meta.get("remote_addr", none),             }              data = urllib.urlencode(values)             req = urllib2.request(url, data)             response = urllib2.urlopen(req)             result = json.loads(response.read())              # result["success"] true on success             if result["success"]:                 return view             else:                 return failure         return failure     return wrap 

and used on view...

@validate_captcha def sendemail(request):     ... 

but attribute error...

traceback: file "/home/jeff/django/langalang/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response   223.                 response = middleware_method(request, response) file "/home/jeff/django/langalang/venv/local/lib/python2.7/site-packages/django/middleware/common.py" in process_response   138.         if response.status_code == 404 , not settings.debug:  exception type: attributeerror @ /ko/contact/sendemail exception value: 'function' object has no attribute 'status_code' 

i think...this happening because returned function, cannot figure out why django treating response, because response handled in decorator , verified made through return point 'result["success"]'

i think should call failure() function have httpresponse returned wrapping decorator. replace:

return failure 

with:

return failure() 

and, same goes view, call it:

return view(request, *args, **kwargs) 

Comments