0

syntax error:

msg = "keyword can't be an expression"
      offset = None
      print_file_and_line = None
      text = 'data = data(name and mood=self.request.POST)\n'

I'm posting much of the code here as even though my Datastore has a "Visitor" Entity with name, mood, date properties (the index.yaml file is working apparently), form data is not being submitted to Datastore as evident in Console query:

SELECT name FROM Visitor
              ^ SyntaxError: invalid syntax

The last section of the following is me guessing what to do from modified Google tutorial. I know it's wrong but hope you see what I'm trying to do:

class Visitor(db.Model):

    name = db.StringProperty(required=1)
    mood = db.StringProperty(choices=["Good","Bad","Fair"]) # this is Radio button
    date = db.DateTimeProperty(auto_now_add=True)

class MainPage(webapp.RequestHandler):
    def get(self):
        self.response.out.write("""<html><body>
        <form action="/" method="post">
            <p>First Name: <input type="text" name="name"/></p>   # text
            <p><input type="radio" name="mood" value="good">Good</p>  # radio button v 
            <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>""")  
    def post(self):
        name = self.request.get("name")
        mood = self.request.get("mood")
        data = data(name and mood=self.request.POST) # < < ^ ^ PROBLEM(S)
        if data.is_valid():
            Visitor = data.save(commit=False)
            Visitor.put()

thanks in advance for help to attain desired goal.

4

1 回答 1

2

正如您所指出的,您的问题出在这一行

data = data(name and mood=self.request.POST) 

语法错误是因为您试图在表达式中进行赋值。

mood=self.request.POST
#"name and mood" is a logical expression which will return 
#"mood" if bool(name) is True and bool(mood) is True
#Otherwise it returns the first False value.
data=data(name and mood)  

当然,这也很有趣,因为 data 可能是一个可调用的,您将用它的结果替换它......

此外,数据没有在任何地方定义(我们可以看到)......因此,虽然我们已经摆脱了一个语法错误,但(可能)还有其他问题潜伏在您的脚本中。

于 2012-06-14T15:02:44.043 回答