7

I'm a web dev. noobie and I'm putting together a simple signup page as practice using Python on GoogleApp Engine.

When the signup form is filled out incorrectly, the program works fine. When it's filled out correctly, it's supposed to redirect to a thank you page but instead I get a server error:

Internal Server Error

The server has either erred or is incapable of performing the requested operation.

Traceback (most recent call last):
  File      "/Users/bendavidow/Desktop/Stuff/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEng  ine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1547,   in __call__
return response(environ, start_response)
TypeError: 'unicode' object is not callable

My question is two-fold: (1) What does this error mean? and (2) Where is it coming from?

Here's the code, minus the html:

import webapp2 
import re
import cgi
def escape_html(s):
    return cgi.escape(s, quote = True)

class MainPage(webapp2.RequestHandler):
    def write_form(self, username = "", email = "", username_error = "", password_error   = "", verify_error = "", email_error = ""):
    self.response.out.write(form %{"username": escape_html(username), "email": escape_html(email), "username_error": username_error, "password_error": password_error, "verify_error": verify_error, "email_error": email_error})

def get(self):
    self.write_form()

def post(self):
    username = self.request.get('username')
    password = self.request.get('password')
    verify = self.request.get('verify')
    email = self.request.get('email')


    username_check = re.search(r'^[a-zA-Z0-9_-]{3,20}$', username)
    password_check = re.search(r'^.{3,20}$', password)
    email_check = re.search(r'^[\S]+@[\S]+\.[\S]+$',email)


    username_error, password_error, email_error, verify_error = "", "", "", ""
    if not username_check:
        username_error = "Invalid Username"
    if not password_check:
        password_error = "Invalid Password"
    if email != '' and not email_check:
        email_error = "Invalid Email"
    if password != verify:
        verify_error = "Passwords do not match"

    if username_error or password_error or email_error or verify_error:
        self.write_form(username, email, username_error, password_error, verify_error, email_error)
    else:
        return username
        self.redirect("/thanks")


class ThanksHandler(webapp2.RequestHandler):
    def get(self):
        self.response.out.write("Welcome " + username)

app = webapp2.WSGIApplication([('/', MainPage), ('/thanks',ThanksHandler)], debug = True)
  • 3
    I suspect somewhere you've accidentally overwritten self.response with a unicode object. When it tries to call it later, it tries to call your string. – Max Jul 31 '12 at 17:41
  • Your, indentation seems fairly broken by the way, and this being Python it's hard for anyone else to tell what your intent was. – millimoose Jul 31 '12 at 17:43

1 Answers1

5

Webapp2 expects returns from handler methods (get/post) to be callable. The reason that is done is so that instead of altering the response object provided to you, (self.response, which in turn modifies the environment of the request) you construct a unit of work in the form of a callable response, which is then applied onto the environment of the request by the framework.

When you return username the framework tries invoking your uncallable unicode object (username).

Novikov
  • 4,399
  • 3
  • 28
  • 36