p1nesap
p1nesap

Reputation: 179

"keyword can't be an expression" Form Post Data to Datastore

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.

Upvotes: 0

Views: 1338

Answers (1)

mgilson
mgilson

Reputation: 309969

Your problem is at this line as you've pointed out

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

The syntax error is because you're trying to do assignment in an expression.

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)  

Of course, this is funny too because data is presumably a callable which you're replacing with it's result...

Also, data isn't defined anywhere (that we can see)...So while we've gotten rid of one syntax error, there are (likely) other problems lurking in your script.

Upvotes: 2

Related Questions