6

我正在尝试制作一个生成以下 HTML 的烧瓶表单:

<input type="text" name="title" class="field">
<textarea class="field"></textarea>
<select name="status">
    <option value="active">Active</option>
    <option value="inactive">Inactive</option>
</select>

到目前为止,由于我是 python 新手,所以我已经走到了这一步。

  {% from "forms/macros.html" import render_field %}
  <form method="POST" action="." class="form">
  {{ render_field(form.title, class="input text") }}

我的问题是,到目前为止,我的标题字段是否正确,假设我有,有人可以解释我如何获得文本区域和选择字段吗?我已经阅读了文档,我发现几乎不可能理解它。

4

1 回答 1

10

在我看来,最好不要在模板中而是在控制器中定义表单。
示例表单定义:

class CommentForm(Form):
    language = SelectField(u'What You Want', choices=CAN_BE_FILLED_LATER_ON)
    code     = TextAreaField()

以后你需要做的就是——

  1. 通过以下方式初始化表单:

    comment_form = CommentForm()
    
  2. 将其传递给模板:

    return render_template('post_show.jinja2.html', comment_form=comment_form)
    
  3. 在模板中渲染表单:

    <div class="form-group" id='cg-{{comment_form.email.id}}'>
       {{comment_form.email.label(class='col-lg-2 control-label',for=comment_form.email.id)}} 
       <div class="col-lg-9"> 
         {{comment_form.email(class='form-control')}} 
         <span class="help-block" id='hl-{{comment_form.email.id}}'></span> 
       </div> 
    </div
    
于 2013-11-12T05:43:10.713 回答