Google AppEngine:表单处理“重复"结构化属性 [英] Google AppEngine: Form handling 'repeated' StructuredProperty

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

问题描述

在设计表单和处理程序时,我如何使用 ndb.StructuredProperty(repeated = True) 属性?考虑这个例子:

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

我有 3 种 ndb.Model 类型:SkilledPerson、他的教育和他的(工作)经验.后两者是 SkilledPerson 的 StructuredProperty 类型.

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()

如何为技术人员实体创建表单?它将显示简单的字段,例如 namebirth(StringProperty 和 DateProperty).此外,它必须显示 EducationExperience StructuredProperty 属性的一组"字段.我会想象表单看起来像这样:

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?

一个教育表格示例:

<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>

推荐答案

使用 Jinja2,您可以创建一个循环来为每个教育和每个体验生成 HTML.

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

您可以使用特殊的 Jinja 变量 loop.index 来分配唯一的名称,例如 education-1...education-4:"name-{{ loop.index }}"

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

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

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

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

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:表单处理“重复"结构化属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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