App Engine webapp.RequestHandler child instances has no self.request during __init__

Posted by grucha on Stack Overflow See other posts from Stack Overflow or by grucha
Published on 2010-06-02T18:50:49Z Indexed on 2010/06/02 18:54 UTC
Read the original article Hit count: 359

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?

© Stack Overflow or respective owner

Related posts about google-app-engine

Related posts about request-headers