Domanda

I wanted to get the values of self.request.get('foo') and etal everytime I create an instance of a class, so I decided to use __init__ constructor. Here's my code:

class archive(handler):
    d = dict(title='',author= '')

    def __init__(self):
        self.d['title'] = self.request.get('title')
        self.d['author'] = self.request.get('author')

class compose(handler):
    def get(self):
        self.render('compose.html')

    def post(self):
        a = archive()

My purpose is, to get rid the repetition of:

title = self.request.get('title')
author = self.request.get('author')

in every def post(self). But the problem is I get a NoneType error:

AttributeError: 'NoneType' object has no attribute 'get'

Obviously, self.request.get('title') returned None. I am just new with Python and Google Appengine.

Thank you guys.

È stato utile?

Soluzione

This is how I managed to fix the problem:

class Archive(object):
    d = dict(title='',author= '')

    def load_d(self):
        r = webapp2.get_request()

        self.d['title'] = r.get('title')
        self.d['author'] = r.get('author')

class Compose(Handler):
    def get(self):
        self.render('compose.html')

    def post(self):
        a = Archive()
        a.load_d()

Altri suggerimenti

I assume you use webapp2.

Your init overrides the init of the webapp2 request handler (super). You can read in the webapp2 docs how to to this:

http://webapp-improved.appspot.com/guide/handlers.html#overriding-init

Take care when you use variables (self.variable) because you can also override variables of the request handler. You can use the request registry.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top