我如何与ndb.StructuredProperty(重复= True)设计表单和处理程序时的属性?考虑这个例子:
我有 3 种 ndb.Model 类型:技术人员, his 教育,和他的(工作)经验。后两者是 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 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()
我将如何创建一个表格技术人员实体?它将显示简单的字段,例如name and birth(字符串属性和日期属性)。此外,它必须显示一组字段教育 and 经验StructuredProperty 属性。我想象表格看起来像这样:
<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>
如何在此表单中包含“教育”和“经验”字段组?
一个例子教育 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。
您可以使用特殊的 Jinja 变量loop.index 来分配唯一的名称,例如 education-1...education-4:"name-{{ loop.index }}"
如果您需要大量的表单处理和验证,可以使用 WTForms。
请参阅文档:http://wtforms.readthedocs.org/en/latest/crash_course.html http://wtforms.readthedocs.org/en/latest/crash_course.html
如果您需要更改表单中的教育和经历列表,则必须使用 javascript 和 jquery 来添加新项目(表单字段)或删除项目(表单字段)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)