title: Flask WTForm render selected with Bootstrap
body: <p>This is a really simple question but I cannot seem to find a good answer.</p>
  
  <p>I have existing data stored about a user's selection and want to render that selection as selected in the dropdown. </p>
  
  <pre><code>{% macro render_bootstrap_field(field) %}
  
  &lt;div class="form-group {% if field.errors %} error {% endif %}"&gt;
      &lt;label class="col-lg-2 control-label"&gt;{{ field.label }}&lt;/label&gt;
  
      &lt;div class="col-lg-8"&gt;
          {{ field(class='form-control')|safe }}
          {% if field.errors %}
              {% for error in field.errors %}
                  &lt;span class="help-inline"&gt;[{{ error }}]&lt;/span&gt;&lt;br&gt;
              {% endfor %}
          {% endif %}
      &lt;/div&gt;
  &lt;/div&gt;
  {% endmacro %}}
  </code></pre>
  
  <p>And here is how this is rendered:</p>
  
  <pre><code>{{ render_bootstrap_field(form.gender) }}
  </code></pre>
  
  <p>It doesn't show what's selected and I cannot think of a way besides using jquery to add the class manually, but that would be a pain.</p>
  
  <p>Thanks so much for the help!</p>
  
  <p>Edit: forgot to include my view.py function! So I did set it</p>
  
  <pre><code>@app.route('/edit_user', methods=['GET', 'POST'])
  @login_required
  def edit_user():
    form = UserInfoForm(g.user.nickname)
    form.gender.choices = app.config['GENDER']
    form.year.choices = app.config['BIRTH_YEAR']
    form.education.choices = app.config['EDUCATION']
  
    if request.method == 'POST' and form.validate_on_submit():
       flash("submitting")
       g.user.nickname = form.nickname.data
       g.user.gender = form.gender.data
       g.user.education = form.education.data
       g.user.year = form.year.data
       g.user.info_complete = True
       db.session.add(g.user)
       db.session.commit()
       return redirect(url_for('edit_user'))
  
    elif request.method != "POST":
       form.nickname.data = g.user.nickname
       form.gender.data = g.user.gender
       form.education.data = g.user.education
       form.year.data = g.user.year
  
  
    return render_template('edit_user.html',
                           form=form,
                           user=g.user)
  </code></pre>
  
  <p>Problem found!</p>
  
  <p>In case you are running into the same problem --- check your encoding! My unicode was throwing normal string off.</p>