views:

169

answers:

1

i use modified webapp.RequestHandler for handling requests in my app:

class MyRequestHandler(webapp.RequestHandler):
    """
    Request handler with some facilities like user.
    self.out is the dictionary to pass to templates 
    """
    def __init__(self, *args, **kwargs):
        super(MyRequestHandler, self).__init__(*args, **kwargs)
        self.out = {
            'user': users.get_current_user(),
            'logout_url': users.create_logout_url(self.request.uri)
            }

    def render(self, template_name):
        """
        Shortcut to render templates
        """
        self.response.out.write(template.render(template_name, self.out))


class DeviceList(MyRequestHandler):
    def get(self):
        self.out['devices'] = GPSDevice.all().fetch(1000)
        self.render('templates/device_list.html')

but I get an exception:

line 28, in __init__
    self.out['logout_url'] = users.create_logout_url(self.request.uri)
AttributeError: 'DeviceList' object has no attribute 'request'

When the code causing exception is moved out of __init__ everything's fine:

class MyRequestHandler(webapp.RequestHandler):
    """
    Request handler with some facilities like user.
    self.out is the dictionary to pass to templates and initially it contains user object for example
    """
    def __init__(self, *args, **kwargs):
        super(MyRequestHandler, self).__init__(*args, **kwargs)
        self.out = { 'user': users.get_current_user(), }

    def render(self, template_name):
        """
        Shortcut to render templates
        """
        self.out['logout_url'] = users.create_logout_url(self.request.uri)
        self.response.out.write(template.render(template_name, self.out))

Whi is that? Why there's no self.request after parent's (i.e. webapp.RequestHandler's) __init__ was executed?

+2  A: 

http://code.google.com/appengine/docs/python/tools/webapp/requesthandlerclass.html#RequestHandler_initialize

initialize(request, response) Initializes the handler instance with Request and Response objects. Typically, the WSGIApplication does this after instantiating the handler class.

Looks like you want to override initialize instead of init if you're expecting the request object to already be populated.

Drew Sears
Thanks :-) (I was looking into docs but apparently not too deep)
grucha