开发者

google app engine - how to get ListProperty from an html form's input type="text"

i have an aplication that takes some parameters through an html form and then creates a model entity. The problem is, that whatever i try, i get an error like this:

BadValueError: Property xxx must be a list

this is the model:

 xxx = db.ListProperty(int)

this is the sentence used for开发者_开发技巧 getting the list:

xxx = self.request.get('xxx')

I figure that the html form returns a string as i hit the submit button. So, how would i be able to get a list from an input type="text" in an html form? If i write 1,2 it's not ok, as isn't anything else.

The python code is similar to the helloworld application where a form is used to post greetings on the page, the difference is that i need to get a list, not text.

self.response.out.write("""
          <form action="/sign" method="post">
            <div><textarea name="content" rows="3" cols="60"></textarea></div>
            <div><input type="submit" value="Sign Guestbook"></div>
          </form>
        </body>
      </html>""")

class Guestbook(webapp.RequestHandler):
    def post(self):
        greeting = Greeting()

        if users.get_current_user():
            greeting.author = users.get_current_user()

        greeting.content = self.request.get('content')
        greeting.put()
        self.redirect('/')

Is this the optimal way to get user input to create a model entity and how can i fix it so that it will get a list and write it in the models attributes?


The solution was very simple, once an expert showed me :)

tlist = map(lambda x: int(x), self.request.get_all('xxx'))


You should put lists inside xxx, not strings or integers.

Maybe you would want to use request.get_all method, instead of get.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜