谷歌应用程序引擎 - 如何从 html 表单的输入类型=“文本”获取 ListProperty
我有一个应用程序,它通过 html 表单获取一些参数,然后创建一个模型实体。问题是,无论我尝试什么,我都会收到这样的错误:
BadValueError: Property xxx must be a list
这是模型:
xxx = db.ListProperty(int)
这是用于获取列表的句子:
xxx = self.request.get('xxx')
我认为当我点击提交按钮时,html 表单会返回一个字符串。那么,我如何才能从 html 表单中的输入 type="text" 获取列表呢?如果我写 1,2 就不行了,其他的也不行。
python代码类似于helloworld应用程序,其中使用表单在页面上发布问候语,不同之处在于我需要获取列表,而不是文本。
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('/')
这是获取用户输入来创建模型实体的最佳方法吗?我如何修复它,以便它将获得一个列表并将其写入模型属性中?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一旦专家向我展示,解决方案非常简单:)
The solution was very simple, once an expert showed me :)
您应该将列表放入
xxx
内,而不是字符串或整数。也许您想要使用 request.get_all 方法,而不是 get。
You should put lists inside
xxx
, not strings or integers.Maybe you would want to use request.get_all method, instead of get.