开发者

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

开发者 https://www.devze.com 2023-01-01 19:21 出处:网络
i use modified webapp.RequestHandler for handling requests in my app: class MyRequestHandler(webapp.RequestHandler):

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(te开发者_运维知识库mplate.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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消