jmtoung
jmtoung

Reputation: 1012

python attribute error: object has no attribute

I have a class 'UserHandler' that inherits from 'RestHandler', but for some reason it's not inheriting properly as I keep getting a 'AttributeError: 'UserHandler' object has no attribute 'sendJson'' error. I'm running python 2.7 btw.

Code and traceback below:

import json
import webapp2

class RestHandler(webapp2.RequestHandler):

  def dispatch(self):
    #time.sleep(1)
    super(RestHandler, self).dispatch()


  def SendJson(self, r):
    self.response.headers['content-type'] = 'text/plain'
    self.response.write(json.dumps(r))

class UserHandler(RestHandler):
  def post(self):
    r = json.loads(self.request.body)
    user = modelUser.InsertUser(r['email'], r['firebaseUid'], r['firebaseId'])
    r = modelUser.AsDict(user)
    self.sendJson(r)

Traceback:

Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 266, in Handle
    result = handler(dict(self._environ), self._StartResponse)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1519, in __call__
    response = self._internal_error(e)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1511, in __call__
    rv = self.handle_exception(request, response, e)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1505, in __call__
    rv = self.router.dispatch(request, response)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1077, in __call__
    return handler.dispatch()
  File "main.py", line 25, in dispatch
    super(RestHandler, self).dispatch()
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 547, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 545, in dispatch
    return method(*args, **kwargs)
  File "main.py", line 37, in post
    self.sendJson(r)
AttributeError: 'UserHandler' object has no attribute 'sendJson'

Upvotes: 2

Views: 8116

Answers (3)

Sazid
Sazid

Reputation: 2827

In your last few lines:

class UserHandler(RestHandler):
  def post(self):
    r = json.loads(self.request.body)
    user = modelUser.InsertUser(r['email'], r['firebaseUid'], r['firebaseId'])
    r = modelUser.AsDict(user)
    self.SendJson(r)
         ^
   Should be capitalized

Hope that helps :D

Upvotes: 3

falsetru
falsetru

Reputation: 369424

The code defined SendJson (uppercase). But subclass call sendJson. (lowercase)

Upvotes: 4

AMacK
AMacK

Reputation: 1396

Capitalize the 's' (or better yet, make the function 'S' lowercase)

Upvotes: 2

Related Questions