Google App Engine:保留“复选框”用户输入

时间:2013-11-26 19:16:44

标签: python forms google-app-engine checkbox app-engine-ndb

考虑一下这个new-food.html:

<form>
  <h2>Food Name</h2>
  <input type="text" name="food" value="%(foodname)s">

  <h2>Sides:<h2>
  <input type="checkbox" name="sides"  value="with_salad"> With Salad
  <input type="checkbox" name="sides"  value="with_fries"> With Fries

  <input type="submit">
</form>

在提交时,它会创建一个 food 实体,并保存这些项目['apple', 'mango']。当我们想要编辑这个实体时会发生什么? %(foodname)s将保留食物名称文本字段,但我们如何保留页面上传的复选框字段,如下所示:

  <input type="checkbox" name="sides"  value="with_fries" checked> With Fries

1 个答案:

答案 0 :(得分:1)

在app引擎Python中,您可以将Jinja2用于服务器端模板:

<form>
  <h2>Food Name</h2>
  <input type="text" name="food" value="{{ foodname }}">

  <h2>Sides:<h2>
  <input type="checkbox" name="sides"  value="with_salad" {% if sides == "with_salad" %}checked{% endif %}> With Salad
  <input type="checkbox" name="sides"  value="with_fries" {% if sides == "with_fries" %}checked{% endif %}> With Fries

  <input type="submit">
</form>

请参阅文档:https://developers.google.com/appengine/docs/python/gettingstartedpython27/templates