Atul
Atul

Reputation: 81

Inheriting webapp2 google App engine error

Hi I have the following code in google Appengine.When get of Rot13 is called I get the following error:

handler = self.handler(request, response)

TypeError: init() takes exactly 1 argument (3 given)

class BaseHandler(webapp2.RequestHandler):
    def no_caches(self):
        self.response.headers["Pragma"]="no-cache"
        self.response.headers["Cache-Control"]="no-cache, no-store, must-revalidate,  pre-check=0, post-check=0"
        self.response.headers["Expires"]="Thu, 01 Dec 1994 16:00:00"

    def render_str(self,template,**params):
        t=jinja_env.get_template(template)
        return t.render(params)

    def render(self,template,**kw):
        self.response.out.write(self.render_str(template,**kw))

    def write(self,*a,**kw):
        self.response.out.write(*a,**kw)

class Rot13(BaseHandler):

  def __init__(self):
    #BaseHandler.__init__(self) not working too
    self.no_caches()          

  file='rto13.html'

  def convert_rto13(self,s):
      return s.encode('rot13')

  def get(self):
      self.render(self.file,text='',status='welcome')

  def post(self):
     text=self.request.get('text')
     if not text:
        return self.render(self.file,text='',status='You have to enter something')
     rot13=self.convert_rto13(text)
     self.render(self.file,text=rot13,status='translated')

Upvotes: 0

Views: 210

Answers (3)

Tim Hoffman
Tim Hoffman

Reputation: 12986

You can't over ride the __init__ handler for RequestHandler in your BaseHandler unless you accept all it's arguments and you call super. Just blindly overriding an __init__ method without investigating what it does in the parent class is a recipe for problems - hence your problem.

RequestHandler for webapp2 is documented. https://webapp-improved.appspot.com/api/webapp2.html#webapp2.RequestHandler

Specifically it says class webapp2.RequestHandler(request=None, response=None)

so your init method in Rot13 which overrides BaseHandler should look like

def __init__(self,request,response):
    super(Rot23,self).__init__(request,response)
    # more stuff here.

Upvotes: 2

vishalg
vishalg

Reputation: 131

The problem lies in the no_caches() function.

def no_caches(self):
    self.response.headers["Pragma"]="no-cache"
    self.response.headers["Cache-Control"]="no-cache, no-store, must-revalidate,  pre-check=0, post-check=0"
    self.response.headers["Expires"]="Thu, 01 Dec 1994 16:00:00"

When entering multiple headers use addheaders(field,value) instead.

def no_caches(self):
    self.response.headers["Pragma"]="no-cache"
    self.response.headers.add_header("Cache-Control","no-cache, no-store, must-revalidate,  pre-check=0, post-check=0")
    self.response.headers.add_header("Expires","Thu, 01 Dec 1994 16:00:00")

https://developers.google.com/appengine/docs/python/tools/webapp/redirects

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599956

As the documentation for RequestHandler shows, it takes two arguments: request and response. Your __init__ needs to accept those too, or even better accept *args, **kwargs, and pass them onto the super class.

Upvotes: 2

Related Questions