Google AppEngine:表单处理“重复”的StructuredProperty [英] Google AppEngine: Form handling 'repeated' StructuredProperty

查看:104
本文介绍了Google AppEngine:表单处理“重复”的StructuredProperty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当涉及到设计表单和处理程序时,如何处理 ndb.StructuredProperty(重复= True)属性?考虑一下这个例子:



我有3种模式: SkilledPerson ,他的 Education 和他的(工作)体验。后两个是SkilledPerson的StructuredProperty类型。

  class SkilledPerson(ndb.Model):
name = ndb.StringProperty ()
birth = ndb.DateProperty()
education = ndb.StructuredProperty(Education,repeated = True)
experience = ndb.StructuredProperty(Experience,repeated = True)

class教育(ndb.Model):
院校= ndb.StringProperty()
认证= ndb.StringProperty()
start = ndb.DateProperty()
完成= ndb.DateProperty()

class Experience(ndb.Model):
job_title = ndb.StringProperty()
workplace = ndb.StringProperty()
start = ndb .DateProperty()
finish = ndb.DateProperty()

如何创建表单对于熟练人员实体?它会显示简单的字段,如 name birth (StringProperty和DateProperty)。此外,它还必须为 体验结构属性属性显示一组字段。我会想象这个表格看起来像这样:

 < form method =post> 

< h2>熟练人员表格< / h2>

< label>名称< br>
< input type =textname =namevalue ={{name}}>
< / label>


< label>出生< br>
< input type =datename =birthvalue ={{birth}}>
< / label>


<! - 教育表单在这里 - >

<! - - 体验表单放在这里 - >

< input type =submit>

< / form>

如何在此表单中包含教育和体验字段组? b
$ b

一个示例 Education 表单:

 < form method = POST > 

< h2>添加教育< / h2>

< label>机构< br>
< input type =textname =institutionvalue ={{institution}}>
< / label>

< label>认证< br>
< input type =textname =certificationvalue ={{certification}}>
< / label>

< label>开始< br>
< input type =datename =startvalue ={{start}}>
< / label>

< label>完成< br>
< input type =datename =finishvalue ={{finish}}>
< / label>

< input type =submit>

< / form>


解决方案

使用Jinja2,您可以创建一个循环来生成HTML您可以使用特殊的Jinja变量loop.index来指定唯一的名称,比如education-1 ... education-4:<$ <

c $ c>name - {{loop.index}}



如果您需要大量表单处理和验证,您可以使用WTForms。
查看文档: http://wtforms.readthedocs.org/en/latest /crash_course.html



如果您需要更改表单中的教育和体验列表,则必须使用javascript和jquery添加新项目(表单域)或删除项目(表单域)。


How do I work with ndb.StructuredProperty(repeated = True) properties when it comes to designing their forms and handlers? Consider this example:

I've got 3 ndb.Model kinds: SkilledPerson, his Education, and his (work) Experience. The latter two are StructuredProperty types of SkilledPerson.

class SkilledPerson(ndb.Model):
    name = ndb.StringProperty()
    birth = ndb.DateProperty()
    education = ndb.StructuredProperty(Education, repeated = True)
    experience = ndb.StructuredProperty(Experience, repeated = True)

class Education(ndb.Model):
    institution = ndb.StringProperty()
    certification = ndb.StringProperty()
    start = ndb.DateProperty()
    finish = ndb.DateProperty()

class Experience(ndb.Model):
    job_title = ndb.StringProperty()
    workplace = ndb.StringProperty()
    start = ndb.DateProperty()
    finish = ndb.DateProperty()

How would I create a form for the Skilled Person entity? It would display simple fields such as name and birth (StringProperty and DateProperty). Additionally, it must display a 'group' of fields for the Education and Experience StructuredProperty properties. I would imagine the form to look something like this:

<form method="post">

<h2>Skilled Person Form</h2>

    <label>Name<br> 
        <input type="text" name="name" value="{{name}}">
    </label>


    <label>Birth<br> 
        <input type="date" name="birth" value="{{birth}}">
    </label>


    <!-- Education form goes here -->

    <!-- and Experience form goes here -->

    <input type="submit">

</form>

How do I include the groups of fields for Education and Experience in this form?

An example Education form:

<form method="post">

<h2>Add Education</h2>

    <label>Institution<br> 
        <input type="text" name="institution" value="{{institution}}">
    </label>

    <label>Certification<br> 
        <input type="text" name="certification" value="{{certification}}">
    </label>

    <label>Start<br> 
        <input type="date" name="start" value="{{start}}">
    </label>

    <label>Finish<br> 
        <input type="date" name="finish" value="{{finish}}">
    </label>

    <input type="submit">

</form>

解决方案

Using Jinja2 you can create a loop to generate the HTML for each Education and for each Experience.

You can use the special Jinja variable loop.index to assign unique names like education-1...education-4: "name-{{ loop.index }}"

If you need a lot of form processing and validation, you can use WTForms. See the docs: http://wtforms.readthedocs.org/en/latest/crash_course.html

And if you need to change the lists of Educations and Experiences in your form, you have to use javascript and jquery to add new items (form fields) or to delete items (form fields).

这篇关于Google AppEngine:表单处理“重复”的StructuredProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆