Search code examples
twitter-bootstrapflaskflask-wtforms

How to incorporate a flask_wtf form into a Bootstrap form?


I am trying to create a simple sign-in/out website, but I don't know how to use Bootstrap with Flask effectively. I have only just started using Bootstrap, so I am new to how it works exactly, and web development in general.

Below is the flask_wtf form I am using:

<h1>Sign In</h1>
<form action="" method="post" novalidate>
    {{ form.hidden_tag() }}
    <p>
        {{ form.username.label }}<br>
        {{ form.username(size=32) }}
    </p>
    <p>
        {{ form.password.label }}<br>
        {{ form.password(size=32) }}
    </p>
    <p>{{ form.remember_me() }} {{ form.remember_me.label }}</p>
    <p>{{ form.submit() }}</p>
</form>
class LoginForm(FlaskForm):
    username = StringField('Username', validators=[DataRequired()])
    password = PasswordField('Password', validators=[DataRequired()])
    remember_me = BooleanField('Remeber Me')
    submit = SubmitField('Sign In')

And a basic Bootstrap form:

<form>
    <div class="form-group">
        <label for="exampleInputEmail1">Username</label>
        <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
    </div>
    <div class="form-group">
        <label for="exampleInputPassword1">Password</label>
        <input type="password" class="form-control" id="exampleInputPassword1">
    </div>
    <div class="form-group form-check">
        <input type="checkbox" class="form-check-input" id="exampleCheck1">
        <label class="form-check-label" for="exampleCheck1">Remember Me</label>
    </div>
    <button type="submit" class="btn btn-primary">Sign In</button>
</form>

I don't understand how to use the Flask-style placeholders in Bootstrap code.

Note: I don't know why the HTML is mostly orange; if anyone could change it so it looks more clear that would be great.


Solution

  • This should work:

    <form action="" method="post" novalidate>
        {{ form.hidden_tag() }}
        <div class="form-group">
            {{ form.username.label }}
            {{ form.username(class_="form-control", size=32) }}
        </div>
        <div class="form-group">
            {{ form.password.label }}
            {{ form.password(class_="form-control", size=32) }}
        </div>
        <div class="form-group form-check">
            {{ form.remember_me(class_="form-control") }} {{ form.remember_me.label }}
        </div>
        {{ form.submit(class_="form-control") }}
    </form>