ravi
ravi

Reputation: 1088

WebApp2: TypeError: get() takes exactly 1 argument (2 given)

After user registration i would like to redirect to a welcome page with url "../user/abcxyz" where abcxyz would be the username. However on redirect page I get the following error:

return method(*args, **kwargs)
TypeError: get() takes exactly 1 argument (2 given)

Following is the relevant part of the code:

class Signup(MainHandler):
  ...
  ...
  # after successful signup redirect to welcome page 
  self.redirect('/user/%s' % username)

class WelcomeHandler(MainHandler):
   def get(self):
       self.render('welcome.html')

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


class MainHandler(webapp2.RequestHandler):
    """ Class for handelling account register, login, etc."""

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

    def render_str(self, template, **params):
        return render_str(template, **params)

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

app = webapp2.WSGIApplication([
(r'/', HomeHandler),
            (r'/user/(.*)', WelcomeHandler),
            (r'/signup', Signup)
        ], debug=True)

I went through many similar questions asked before but not found any answer relevant to my problem. Please help me fix this error.

Upvotes: 0

Views: 1303

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174696

It's because of the capturing group, that you have used in the route regex.

(r'/user/(.*)', WelcomeHandler),

this would capture the string which follows /user/ and then passed to the corresponding request method get or post as second parameter.

So you have to modify the request method exists in the corresponding handler like below.

class WelcomeHandler(MainHandler):
   def get(self, username):
       self.render('welcome.html')

if you want to pass the username to the welcome.html page, then

def get(self, username):
    self.render('welcome.html', username=username)

Upvotes: 1

Related Questions