how to add class to label in quick_form flask bootstrap
Asked Answered
S

1

6

Using quick_form Flask-Bootstrap to generate the form is very convenient for me, however, I could not find in their documentation a way that I can add extras class to label as required by my template theme.

My forms.py class look like this:

from flask_wtf import Form
from wtforms import StringField,PasswordField,TextField,SubmitField
from wtforms.validators import InputRequired,EqualTo,Email,ValidationError

class Building_Form(Form):
    BuildingName = TextField('Building Name')
    BuildingType = TextField('Building Type')
    FloorNums    = TextField('Numers of Floor')
    BuildUnits   = TextField('Units in Building')
    BuildSize    = TextField('Building Size')
    BuiltYear    = TextField('Built in (year)')
    BuildOpeningTime = TextField('Open Time')
    BuildClosingTime = TextField('Close Time')

My routes.py is like following:

from app.import_core import *
from flask_wtf import FlaskForm
from wtforms import Form, BooleanField, StringField, PasswordField, validators
from wtforms.validators import DataRequired
from flask_wtf.file import FileField, FileRequired
from werkzeug.utils import secure_filename
from werkzeug.security import generate_password_hash, check_password_hash


from .forms import Building_Form
# from app.user.models import TBL_USER

import pdb

@app.route('/building')
def show_building_listing():

    return render_template('building_listing.html')

@app.route('/builing/new',methods=['GET','POST'])
def add_new_builing():
    form = Building_Form()
    if request.method=='POST':
        return '<h1>Ok Building added</h1>'

    return render_template('new_building.html',form=form)

My new_building.html to render a form:

{% extends "__dashboard.html" %}
{% import "bootstrap/wtf.html" as wtf %}

{% block include_css %}
  <link rel="stylesheet" href="{{url_for('static',filename='css/login.css')}}">
  <link rel="stylesheet" href="{{url_for('static',filename='css/bt-social-button/bootstrap-social.css')}}">
{% endblock %}

{% block content %}

  <div class="card">
    <div class="card-body">
      {{ wtf.quick_form(form, extra_classes='bmd-label-floating') }}
    </div>
  </div>
{% endblock %}

I would like to add classes bmd-label-floating to my form label however it ended up with those classes in <form> instead.

How can I add that class my labels? Thanks.

Shakeup answered 28/4, 2018 at 13:2 Comment(0)
G
5

I had a similar problem. A list of items was used to generate a form with multiple checkboxes. I needed to add a label class for styling them horizontally. I was able to loop through my form object and change class for the form field.label.

<form action="" method="post">

{% with test = form.example %}
{% for field in test %}
    {{ field.label(class_='checkbox-inline') }}
    {{ field }}
{% endfor %}
{% endwith %}

<p>{{ form.submit() }}</p>
</form>
Gait answered 5/6, 2018 at 23:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.