Вопрос

I am trying to make a flask form which produces the following 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>

So far, since I am new to python, I got this far.

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

My question is, have I got this correct so far for the title field, and assuming I have, could someone please explain how I can get a textarea and selectfield? I have read the docs and I am finding it almost impossible to get my head around it.

Это было полезно?

Решение

In my opinion it's better to define the form not in the template but in the controller.
Example form definition :

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

All you need later on is to -

  1. Initialize the form by:

    comment_form = CommentForm()
    
  2. Passing it to the template:

    return render_template('post_show.jinja2.html', comment_form=comment_form)
    
  3. Render the form in the template:

    <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
    
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top