When I run this code in the dev_appserver it gives me the “Invalid syntax” error at line 22, where the HugAPanda class is initialized. Does anybody know why this would happen? Here’s the code:
import wsgiref.handlers
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
class PandasHugs(db.Model):
message = db.IntegerProperty()
class MainPage(webapp.RequestHandler):
def get(self):
ListOfHugs = db.GqlQuery("SELECT * FROM PandasHugs")
Adder = 0
for PandasHugs in ListOfHugs:
Adder = Adder + 1
self.response.out.write('<html><body>')
self.response.out.write('<h6>Panda has ' + str(Adder) + ' hugs!</h6>')
self.response.out.write("<form action=\"/HugPanda\" method=\"post\"><div><input type=\"text\" name=\"PandaMessage\" value=\"A message for a panda.\"></div><div><input type=\"submit\" value=\"Hug a panda?\"></div></form></body></html>">
class HugAPanda(webapp.RequestHandler):
def post(self):
HugForAPanda = PandaHugs()
HugForAPanda.message = self.request.get('PandaMessage')
HugForAPanda.put()
self.redirect('/main')
application = webapp.WSGIApplication(
[('/', MainPage), ('/main', MainPage), ('/HugPanda', HugAPanda)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
Thanks again!
-Neil
You have invalid syntax in a line above. (Line 15 it looks like)
Extra ‘>’ at the end should be replace with ‘)’.
This is a very good reason to follow python convention and limit line length to 79 characters. I won’t argue with going up to 120 if that is standard at your organization, but it certainly should not be written the way it’s presented here 🙂
In this case I’d recommend writing readable html code (ie. properly indented) in triple quotes. In your case I would use single triple quotes so you do not have to escape every “. I just recommend the single quotes here to avoid confusion, but I believe “”” will also work in this case.
ie.
Just noticed some errors in the html after rewriting your code should have ‘/>’ to close your input tags. Good style can go a long way in avoiding errors without the use of any tools!
http://www.python.org/dev/peps/pep-0008/