【问题标题】:Google App Engine: Preserving 'checkbox' user inputGoogle App Engine:保留“复选框”用户输入
【发布时间】:2013-11-27 07:34:02
【问题描述】:

考虑一下这个 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 会保留Food Name 文本字段,但是我们如何保留页面上传的checkbox 字段,像这样:

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

【问题讨论】:

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


    【解决方案1】:

    在应用引擎 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

    【讨论】:

    • 谢谢@voscausa。从您的个人资料中,我注意到您对 GAE 非常了解。如果您不介意,我也希望您能在这个问题上提供帮助:stackoverflow.com/questions/20025241/…。我希望为发展中国家建立一个急需的就业市场。
    猜你喜欢
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-06
    • 2015-11-13
    • 1970-01-01
    • 2020-01-31
    • 2012-06-20
    相关资源
    最近更新 更多