I want the user’s input to eventually be modeled with a db.stringListProperty(). What would the input format have to look like and how would the user’s inputs have to be constructed (with []’s, with quotes, with commas, etc.), if this is possible? I am using jinja2 for form templates, if that matters. (Extra credit for how to validate that the data was input correctly .)
Thanks.
I get this error from appengine.
raise BadValueError('Property %s must be a list' % self.name)
BadValueError: Property choices must be a list
The error occurs on this line below.
person.choices = self.request.get(‘choices’)
class PQ(db.Model):
choices = db.StringListProperty()
key = db.Key.from_path("PQ", ID_id)
person = PQ.get(key)
template_values = {'ID_id':person.key(),
'p': person,
}
path = os.path.join(TEMPLATE_DIR, 'add_person.html')
self.response.out.write(template.render(path, template_values))
class AddPerson(BaseHandler):
def post(self):
ID_id = self.request.get('ID')
key = db.Key.from_path("PQ", ID_id)
person = PQ.get(key)
person.choices = self.request.get('choices')
person.put()
self.redirect("/?ID=%s" % ID_id)
add_person.html
<html>
<body>
<div id="inputdata">
<form action="add_person" method="post" enctype="multipart/form-data">
<label>Enter your list of choices. Your list must follow this format:
["Less","Same","More"] </label>
<input type="textbox" name="choices" size="30" value="{{ p.choices }}"></input><br/>
<input type="submit" value="Submit"></input>
</form>
</div>
</body>
</html>
It seems that you get this error because
choicesmust be a list type, but the value that you get from your request is a string – and it’ll always be, since you get it from your HTTP request.On the form side, it doesn’t matter what format the user types data in, since it’s always passed as a string – a comma-separated list should suffice. Then you could parse it with e.g.
self.request.get('choices').split(",")(and trim whitespace on your side so you don’t bother the user with exact whitespace requirements).Best practices dictate that you should assist the user as much as possible – if you have a set of pre-defined choices, you could use Select2 or a similar widget to guide the user with making their selection.