the cgi.escape section (below) throws traceback in dev server, but when I comment out cgi.escape, I get syntax error on RequestHandler. I’m following Google’s tutorial, which includes 2 submit buttons for 1 form, which is unclear to me, as is the /sign? form action; my form below is 1 (not 2) submit button, for combined text and radio field form, so I’m unsure if I’m doing the last section properly or even the form. I’ve got all the imports for this to work. Got to get these user form values in Datastore, spreadsheets, or something, anything…help is greatly appreciated.
Traceback (most recent call last): Fil "/home/paul/Desktop/google_appengine/google/appengine/ext/webapp/_webapp25.py", line 701, in __call__
handler.get(*groups) File "/home/paul/Desktop/emot/index.py", line 39, in get
cgi.escape(name))) TypeError: not all arguments converted during string formatting
Code:
name=self.request.get("name")
self.response.out.write("""
<form action="/sign?/s" method="post">
<p>First Name: <input type="text" name="name"/></p>
<p><input type="radio" name="mood" value="good">Good</p>
<p><input type="radio" name="mood" value="bad">Bad</p>
<p><input type="radio" name="mood" value="fair">Fair</p>
<p><input type="submit"value="Process"></p>
</form>
</body>
</html>""" % (urllib.urlencode({"name": name}),
cgi.escape(name))) # < < < no string convert HERE
class Info(webapp.RequestHandler): # < < Syntax Error when ^ ^ is commented out.
def post(self):
name = self.request.get("name")
visitor = Visitor(parent=info_key(name))
visitor.content = self.request.get("mood")
visitor.put()
self.redirect("/?" + urllib.urlencode({"name": name}))
application = webapp.WSGIApplication([
("/", MainPage), # < < v v Unsure about these...2 submits?
("/sign", Info)
], debug=True)
This is where you’re running into the problem. Note that this entire chunk of code is wrapped in a function call:
Let’s take out the function call for now, as that’s not really the issue here. Note that I’ll also remove the extra trailing bracket that would finish the function call. This is the reason you were getting a syntax error when you commented out that last line – you were also commenting out the close bracket for the function call, and the interpreter won’t allow the
defining of new classes until it’s complete.That leaves us with this:
What we’re left with is a string formatting operation, of the format
"""string literal""" % (tuple, of, values). For this operation to work, you need to include specifiers (I find ‘hooks’ a slightly more descriptive term) for each of the values you’d like to include in the string. These will always start with a percent sign, and have one or more characters following. You can read more about it at the Python Docs, but a few quick examples include%dfor an integer,%sfor a string, and%ffor a float value.Your code example has two arguments for this operation,
urllib.urlencode({"name": name}), andcgi.escape(name). However, your string doesn’t contain any specifiers (%s, etc) for the interpreter to understand where you’d like to put them.As I see it, you have two choices – you can either remove the arguments all together (but make sure you leave a trailing bracket for the
self.response.out.writecall!), or put in the hooks where you need them.