Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask wtform RadioField label does not render

When using the following form:

class TextForm(Form):
    example = RadioField('Choice 1:', choices=[('A','Option A'),('B','Option B')])
    key = RadioField('Choice 2:', choices=[('1', 'Option 1'), ('2', 'Option 2')])
    submit = SubmitField('Submit')

I am expecting to see:

Choice 1:

  • Option A
  • Option B

Choice 2:

  • Option 1
  • Option 2

Instead I am getting no labels as follows:

  • Option A
  • Option B
  • Option 1
  • Option 2

What am I missing?

like image 598
zanzu Avatar asked Dec 14 '22 17:12

zanzu


1 Answers

I just had the same issue.

It might be intended by the author of "quick_form" macro, or more likely he/she missed a line of code to render out the label of RadioField, as the same is done for other types of fields.

To hack it, locate the file "bootstrap/wtf.html", where macro "quick_form" is defined.

add this line:

{{field.label(class="control-label")|safe}}

before the "for" loop:

{% for item in field -%}
  <div class="radio">
    <label>
      {{item|safe}} {{item.label.text|safe}}
    </label>
  </div>
{% endfor %}

Hope this works for you.

like image 120
John Jiang Avatar answered Dec 31 '22 14:12

John Jiang